public
Description: Daemon Kit aims to simplify creating Ruby daemons by providing a sound application skeleton (through a generator), task specific generators (jabber bot, etc) and robust environment management code.
Homepage: http://kennethkalmer.github.com
Clone URL: git://github.com/kennethkalmer/daemon-kit.git
daemon-kit / Logging.txt
100644 97 lines (65 sloc) 3.017 kb
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
= Logging from inside your daemon
 
Proper logging inside your daemon process is critical, and daemon-kit
provides you with a great logging facility to (ab)use.
 
== Logging examples
 
From anywhere in your code you can access the
<em>DaemonKit.logger</em> instance, which is a configured
DaemonKit::AbstractLogger. It is compatible with Ruby's Logger class
but is much more verbose to help you gain some insight into your
running process.
 
  DaemonKit.logger.info( "Hello world" )
 
This logs a 'Hello world' line to the log file, complete with the
calling file name and line number. Log lines look something like this:
 
  2009-06-07 23:21:30.248575 capd(32513) [INFO] initializer.rb:91: DaemonKit (0.1.7.4) booted, now running capd
 
Log as much as you can, but be careful not to abuse the <em>info</em>
level since your log files can become huge. For general processing
hints, use the <em>debug</em> level.
 
To log exceptions, use the special <em>exception</em> helper:
 
  begin
    # dangerous operation
  rescue => e
    DaemonKit.logger.exception( e )
  end
 
== Controlling logging in a running process
 
Logging can be controlled in a running process, either via code or by
sending UNIX signals to the running process.
 
=== Changing log levels in your code
 
Log levels can be toggled with the <em>level=</em> method:
 
  DaemonKit.logger.level = :info
 
Alternatively you can silence all the logging activity for a while
using the silence helper:
 
  DaemonKit.logger.silence do |logger|
    # logger will only report :error or higher levels
  end
 
You can also set the logging per environment by editing the correct file in <em>config/environments</em>
 
  config.log_level = :debug
 
=== Changing log levels via UNIX signals
 
Send your process the +USR1+ signal to toggle between <em>:debug</em>
and <em>:info</em> log levels. Sending a +USR2+ signal will force the
logger into <em>:debug</em> mode (+USR1+ will revert).
 
== Support for log rotation
 
Support for logrotate is baked right into daemon-kit. By sending your
daemon a +HUP+ signal all the log files file be closed and re-opened
again on first use. Here is an example logrotate configuration:
 
  /path/to/daemon.log {
    rotate 5
    weekly
    postrotate
      kill -HUP `cat /path/to/daemon.pid`
    endscript
  }
 
== Support for syslog logging
 
If you have the
SyslogLogger[http://seattlerb.rubyforge.org/SyslogLogger/] gem
installed, you can have your process log to a UNIX syslog server. You
can change the logging to syslog by either updating your
<em>environment.rb</em> file like this:
 
  config.log_path = :syslog
 
Or by passing 'syslog' as the logfile argument when starting a daemon
 
  $ ruby ./bin/daemon start -l syslog
 
The SyslogLogger rdoc's provide configuration examples for configuring
various UNIX syslog servers.
 
== More logging information
 
If you're running your daemon in the foreground (with the <em>run</em>
command, you'll get copies of all the log messages on STDOUT, and thus
voiding the need to tail log files the whole time.