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
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