log4r configuration: "/dev/null" outputter question

R

Ronald Fischer

Well, my code does seem to work, but I have the feeling that I could
have
done it much simpler.... So here is my problem:

I would like to control log4r via an environment variable, which can
contain one of 4 types of values:

stdout : logging goes to stdout
stderr : logging goes to stderr
file:FILENAME : logging goes to FILENAME
none : logging not needed

The problem is what outputter to use to implement 'none'. My first idea=20
to simply use FileOutputter('none',:filename =3D> '/dev/null') does not
work
because my code is also supposed to run onder Windows where we don't
have
/dev/null. So I came up with the solution to invent a "NullOutputter":

class NullOutputter <Outputter
def initialize(name)
super(name)
end
end

....

$log =3D Logger.new('TestLog')
outputterspec=3DENV['TFW_LOG'] || 'stdout'
$log.outputters =3D case outputterspec
when 'stdout'
Outputter.stdout
when 'stderr'
Outputter.stderr
when /^file:(.+)$/
FileOutputter.new 'tfw', :filename =3D> $1, :trunc =3D> true
when 'none'
NullOutputter.new 'tfwnull' =20
end

Note that I did NOT use

when 'none'
Outputter.new 'tfwnull'

because Outputter is documented as abstract class.

My solution works, but I wonder whether I could make it simpler=20
(i.e. without creating the NullOutputter class).


Ronald
--=20
Ronald Fischer <[email protected]>
Phone: +49-89-452133-162
 
T

Tim Pease

The problem is what outputter to use to implement 'none'. My first idea
to simply use FileOutputter('none',:filename => '/dev/null') does not
work
because my code is also supposed to run onder Windows where we don't
have
/dev/null.

There is a /dev/null equivalent on Windows, NUL: You can use it in
this fashion ...

fn = test(?e, '/dev/null') ? '/dev/null' : 'NUL:'
fd = File.open(fn, 'w')

You can now write to that file descriptor and everything will go to
the bit bucket.
My solution works, but I wonder whether I could make it simpler
(i.e. without creating the NullOutputter class).

Certainly. Just create an instance of Outputter and then define the
methods you need.

out = Outputter.new 'tfwnull'
class << out
def canonical_log(logevent) nil end
end

The canonical_log method is used by the outputters to format the log
event and then write the formatted output to a file. By setting this
to have a nil body, you will save a few cycles each time a log
messages needs to be generated.

Another options is just to set the global log level to 'off'. That is
left as an exercise for the reader ;-)

Blessings,
TwP
 
R

Ronald Fischer

My solution works, but I wonder whether I could make it simpler
=20
Certainly. Just create an instance of Outputter and then define the
methods you need.
=20
out =3D Outputter.new 'tfwnull'
class << out
def canonical_log(logevent) nil end
end

Now I wonder, since Outputter is documented as *abstract* class, why
I can instantiate it at all? Or is "abstract class" in Ruby not dealt
with the same strength as in, say, Java or C++?

But anyway, I tried your approach, but I don't see how I get the
instance
of the anonymous class into my outputters. When I do it like this:

#!/usr/bin/ruby
require 'log4r'
include Log4r
$log=3DLogger.new('testing')
out =3D Outputter.new 'tfwnull'
$log.outputters=3Dclass << out
def canonical_log(logevent) nil end
end

I get the error message

"Expected kind of Outputter, got NilClass (TypeError)"

Ronald
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,165
Latest member
JavierBrak
Top