File: simpleconfig.rb

package info (click to toggle)
ruby-log4r 1.1.10-4.1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, sid, trixie
  • size: 648 kB
  • sloc: ruby: 2,744; xml: 96; makefile: 5
file content (39 lines) | stat: -rw-r--r-- 1,393 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# Simple configuration example.
# Where we configure just one logger and make it log to a file and stdout.

# add the path to log4r if it isn't installed in a ruby path
$: << File.join('..','lib')
require "log4r"

# First things first, get the root logger and set its level to WARN.
# This makes the global level WARN. Later on, we can turn off all logging
# by setting it to OFF right here (or dynamically if you prefer)
Log4r::Logger.root.level = Log4r::WARN

# Remember: By specifying a level, we are saying "Include this level and
# anything worse." So in this case, we're logging WARN, ERROR and FATAL

# create a logger
log = Log4r::Logger.new("simpleconf")

# We want to log to $stderr and a file ./tmp.log

# Create an outputter for $stderr. It defaults to the root level WARN
Log4r::StderrOutputter.new 'console'
# for the file, we want to log only FATAL and ERROR and don't trunc
Log4r::FileOutputter.new('logfile', 
                         :filename=>'logs/simple.log', 
                         :trunc=>false,
                         :level=>Log4r::FATAL)

# add the outputters (this method accepts outputter names or references)
log.add('console','logfile')

# Now let's try it out:
log.debug "debugging"
log.info "a piece of info"
log.warn "Danger, Will Robinson, danger!"
log.error "I dropped my Wookie! :("
log.fatal "kaboom!"

# now run this and compare output to ./tmp.log