kristianmandrup / log4r-color

log4r now with color outputs to terminal/console
Other
6 stars 2 forks source link

how to enable colorization via yml file? #5

Open litebito opened 11 years ago

litebito commented 11 years ago

hi,

I'm a beginner in ruby and I've following question. I was using log4r, but wanted de console output to be colorized, so I checked out this project. but I don't seem to be able to get it working properly

I've my basic config for log4r(-color) in "log4r_config.yml" and it looks like this :

log4r_config:

define all loggers ...

the standard logging levels here can be overridden from the app calling the logger

loggers:

how can I add colorization to the stdoutputter ?

in my script I've something like this :

log4r_config = Log4r::YamlConfigurator log4r_config["_logfile"] = File.basename(FILE, File.extname(FILE)) load_log4r = YAML.load_file("#{File.dirname(FILE)}/config/log4r_config.yml") YamlConfigurator.decode_yaml( load_log4r['log4r_config'] ) mylogger = Log4r::Logger[env] ## -->> logger is selected based on ruby environment

and I tried :

ColorOutputter.new 'color', {:colors => { :debug => :light_blue, :info => :light_blue, :warn => :yellow, :error => :red, :fatal => {:color => :red, :background => :white} } }

mylogger.add('color')

but that adds a 2nd line for each "log line" in de output

I would like to specify the colors in the yml file, is that possible? if not : update my defined logger so it is colorized, without doubling de lines

Thanks for any help

kristianmandrup commented 11 years ago

I created this gem by forking and patching a previous gem. I suggest you do the same. Fork it then clone your forked repo, then reference your clone from your Gemfile, using the :path attribute pointing to the location of your fork on your local disk. Then debug from there until it works the way you like. Cheers!

litebito commented 11 years ago

hmm that would be 1 step beyond my basic knowledge for now. So I guess I'll live without colors for now.

kristianmandrup commented 11 years ago

i think log4r-color was one of my first gems. At least it was for sure the first time I wanted to use a gem and I didn't work or didn't suit my purpose. Was a good exercise making it work...

Perhaps look at this for how to do the Yaml to hash:

http://stackoverflow.com/questions/800122/best-way-to-convert-strings-to-symbols-in-hash

Or use the hashie gem

https://github.com/intridea/hashie

The color outputter functionality resides here:

https://github.com/kristianmandrup/log4r-color/blob/master/lib/log4r-color/outputter/consoleoutputters.rb

  @out.print data.colorize(colors[key])
  @out.flush
  @out.print "".uncolorize

Also look at log4r-color/outputter/iooutputter which is the base outputter. Perhaps the bug is here? Could be related to ruby version??