log2mail 0.0.1.pre2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +19 -0
- data/.rspec +2 -0
- data/.travis.yml +6 -0
- data/Gemfile +4 -0
- data/INSTALL +51 -0
- data/LICENSE.txt +22 -0
- data/README.md +123 -0
- data/Rakefile +20 -0
- data/bin/log2mail.rb +4 -0
- data/features/configtest-mode.feature +22 -0
- data/features/log2mail_configurations/config_1 +7 -0
- data/features/step_definitions/log2mail_steps.rb +41 -0
- data/lib/ext/kernel.rb +13 -0
- data/lib/ext/main.rb +24 -0
- data/lib/ext/string.rb +23 -0
- data/lib/ext/syslog_logger.rb +208 -0
- data/lib/log2mail/config.rb +216 -0
- data/lib/log2mail/console/commands.rb +56 -0
- data/lib/log2mail/console/logger.rb +23 -0
- data/lib/log2mail/console.rb +35 -0
- data/lib/log2mail/error.rb +5 -0
- data/lib/log2mail/file/parser.rb +49 -0
- data/lib/log2mail/file.rb +95 -0
- data/lib/log2mail/hit.rb +13 -0
- data/lib/log2mail/logger_formatter.rb +30 -0
- data/lib/log2mail/main.rb +148 -0
- data/lib/log2mail/report.rb +92 -0
- data/lib/log2mail/version.rb +6 -0
- data/lib/log2mail/watcher.rb +61 -0
- data/lib/log2mail.rb +8 -0
- data/log2mail.gemspec +35 -0
- data/man/log2mail.1 +121 -0
- data/man/log2mail.1.html +207 -0
- data/man/log2mail.1.ronn +108 -0
- data/spec/factories.rb +101 -0
- data/spec/log2mail/config_spec.rb +103 -0
- data/spec/log2mail/file/parser_spec.rb +61 -0
- data/spec/log2mail/report_spec.rb +63 -0
- data/spec/log2mail/watcher_spec.rb +93 -0
- data/spec/spec_helper.rb +112 -0
- metadata +260 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 49c942792863111482748c2728b262aac98222c7
|
4
|
+
data.tar.gz: b6dbfb965e80cc032ad80cf2e9c208be7847ad1c
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 06740c4944bfe2cd8f6f4d0ecfa01321c84ce99d2efc1c308b0055bf6f8dfa7ec0d0e495a7ca546106fdacfd501d28233b34376a226d1ca1be3f534c5eae134a
|
7
|
+
data.tar.gz: 27bf8b30600f937341290214b5f60dd8610b47a8ec9ac2240e1531aa33df68d55d518819f5504e99b9dfca1e4103ea646fdf1d17ad0f67457e72cbfcd37720d4
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/INSTALL
ADDED
@@ -0,0 +1,51 @@
|
|
1
|
+
log2mail.rb Installation
|
2
|
+
========================
|
3
|
+
|
4
|
+
Requirements
|
5
|
+
------------
|
6
|
+
|
7
|
+
* Ruby 1.9.3
|
8
|
+
|
9
|
+
Debian 7 (and higher)
|
10
|
+
---------------------
|
11
|
+
|
12
|
+
To install Ruby:
|
13
|
+
|
14
|
+
sudo apt-get install ruby
|
15
|
+
|
16
|
+
To get this pre-release version of log2mail.rb:
|
17
|
+
|
18
|
+
rbenv shell system # if using rbenv
|
19
|
+
sudo gem install log2mail --pre
|
20
|
+
|
21
|
+
Next Steps
|
22
|
+
----------
|
23
|
+
|
24
|
+
Create a configuration file, e.g. at `/usr/local/etc/log2mail.conf`:
|
25
|
+
|
26
|
+
cat > /usr/local/etc/log2mail.conf <<EOF
|
27
|
+
defaults
|
28
|
+
mailto = your@mail.address
|
29
|
+
file = /tmp/test.log
|
30
|
+
pattern = test
|
31
|
+
EOF
|
32
|
+
|
33
|
+
Test the configuration:
|
34
|
+
|
35
|
+
log2mail.rb configtest --config /usr/local/etc/log2mail.conf
|
36
|
+
|
37
|
+
should return:
|
38
|
+
|
39
|
+
+---------------+---------+-------------------+----------+
|
40
|
+
| File | Pattern | Recipient | Settings |
|
41
|
+
+---------------+---------+-------------------+----------+
|
42
|
+
| /tmp/test.log | test | your@mail.address | |
|
43
|
+
+---------------+---------+-------------------+----------+
|
44
|
+
|
45
|
+
Run *log2mail.rb* in foreground (`-N`) in verbose mode (`-v`) and 10 seconds sleep time in the processing loop:
|
46
|
+
|
47
|
+
log2mail.rb start --config /usr/local/etc/log2mail.conf -Nv --sleeptime 10
|
48
|
+
|
49
|
+
`echo "a first test" >> /tmp/test.log` should result in an email report after few seconds.
|
50
|
+
|
51
|
+
See `gem man log2mail` for more information.
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Markus Strauss
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,123 @@
|
|
1
|
+
log2mail.rb(1) -- monitors (log) files for patterns and reports hits by mail
|
2
|
+
============================================================================
|
3
|
+
|
4
|
+
## SYNOPSIS
|
5
|
+
|
6
|
+
`log2mail.rb` (start|stop|status|configtest) [<var>options</var>]:
|
7
|
+
|
8
|
+
## DESCRIPTION
|
9
|
+
|
10
|
+
`log2mail.rb` helps having an eye on your systems' log files. It efficiently monitors multiple files and reports as soon as specified (regular expression) patterns match.
|
11
|
+
|
12
|
+
On startup, `log2mail.rb` opens all files on the 'watch list' and seeks to EOF. All new data are parsed about once a minute (see `--sleeptime`). <!-- If necessary, i.e. when multiline patterns are set for the file, every new data is put into a fixed-size buffer. The buffer is rolled over when it gets full. --> Matched patterns are reported to the configured mail address(es) (see `mailto` configuration option).
|
13
|
+
|
14
|
+
Log files are reopened automatically when rotated.
|
15
|
+
|
16
|
+
`log2mail.rb` is a pure ruby clone of [log2mail](https://packages.debian.org/squeeze/log2mail) which supports most of the original's features and configuration syntax and adds multiline regular expression matching. Actually it should be possible to use `log2mail.rb` with your existing configuration you may have for log2mail(8).
|
17
|
+
|
18
|
+
## OPTIONS
|
19
|
+
|
20
|
+
* `--config`=<var>path</var>, `-c` <var>path</var>:
|
21
|
+
Specifies the configuration file or directory path. If <var>path</var> is a directory, all files (except such ending in `~` or `#`) are parsed in sorted order. Sorting is by character code, i.e. 0-9 before A-Z followed by a-z.
|
22
|
+
Default value: `/etc/log2mail/conf`.
|
23
|
+
This can also be set by environment variable `LOG2MAIL_CONF`.
|
24
|
+
|
25
|
+
* `--sleeptime`=<var>seconds</var>:
|
26
|
+
Specifies at which interval (in seconds) the log files are parsed. Default value: 60.
|
27
|
+
|
28
|
+
## ENVIRONMENT
|
29
|
+
|
30
|
+
`log2mail.rb` uses the environment variable `LOG2MAIL_CONF`, if present (see option `--config`). The value supplied by option takes precedence.
|
31
|
+
|
32
|
+
## CONFIGURATION (OLD-STYLE)
|
33
|
+
|
34
|
+
The old-style configuration syntax is directly cloned from log2mail(8)'s behavior and should be mostly compatible. It may seem a bit awkward first, but this is how it works: There are two possible top-level 'sections', `defaults` and `file=`<var>path-to-log-file</var> sections. The only statement allowed after a `file=...` section are one or more `pattern=`<var>pattern</var> entries. After the `pattern=...` there may be one or more `mailto=`<var>single-mail-recipient</var> entries. After each `mailto=...` there may be options for that recipient. Also, these options are set from the special `defaults` section, if present (usually it is).
|
35
|
+
|
36
|
+
The basic layout looks like follows:
|
37
|
+
|
38
|
+
# comments start with pound sign (aka hash or number sign)
|
39
|
+
|
40
|
+
defaults
|
41
|
+
fromaddr = DEFAULT FROMADDR
|
42
|
+
sendtime = DEFAULT SENDTIME # seconds
|
43
|
+
resendtime = DEFAULT RESENDTIME # seconds
|
44
|
+
maxlines = DEFAULT MAXLINES # number of lines
|
45
|
+
template = DEFAULT TEMPLATE # filename or path
|
46
|
+
sendmail = DEFAULT SENDMAIL # path to executable with arguments
|
47
|
+
mailto = DEFAULT RECIPIENT # new to log2mail.rb
|
48
|
+
# awkward, not recommended, but possible:
|
49
|
+
pattern = DEFAULT PATTERN # this pattern would be applied to every file
|
50
|
+
mailto = DEFAULT RECIPIENT for previous DEFAULT PATTERN
|
51
|
+
|
52
|
+
# one or more file sections follow
|
53
|
+
file = FILENAME
|
54
|
+
|
55
|
+
# each file can have one or more patterns
|
56
|
+
pattern = PATTERN
|
57
|
+
|
58
|
+
# each pattern can have one or more mailto recipients
|
59
|
+
# each recipient gets its own mailto=... statement
|
60
|
+
mailto = MAIL
|
61
|
+
|
62
|
+
# every option NOT stated here is supplied from defaults
|
63
|
+
fromaddr = ...
|
64
|
+
sendtime = ...
|
65
|
+
resendtime = ...
|
66
|
+
maxlines = ...
|
67
|
+
template = ...
|
68
|
+
sendmail = ...
|
69
|
+
|
70
|
+
# "include" includes the contents of file at the exact place of the
|
71
|
+
# include statement
|
72
|
+
include = PATH TO FILE
|
73
|
+
|
74
|
+
Note that indentation is done for readability purposes only. It serves no role syntactically.
|
75
|
+
|
76
|
+
Splitting the configuration into multiple files is possible, and convenient when using automation tools to distribute settings. In opposition to classic log2mail, with `log2mail.rb` it does not matter at which place the `defaults` section is parsed. Keep in mind though, that later definitions may override earlier ones. In that case a warning is logged.
|
77
|
+
|
78
|
+
## CONFIGURATION (NEW-STYLE)
|
79
|
+
|
80
|
+
None (yet). More features might warrant a new configuration syntax.
|
81
|
+
|
82
|
+
## SECURITY CONSIDERATIONS
|
83
|
+
|
84
|
+
It is neither necessary nor recommended to run this software as root.
|
85
|
+
|
86
|
+
## BUGS
|
87
|
+
|
88
|
+
Configuration options `sendtime`, `resendtime`, `maxlines` not implemented yet. Every match produces a single mail which is sent out immediately - which could produce a lot of mails.
|
89
|
+
|
90
|
+
## HISTORY
|
91
|
+
|
92
|
+
December 2014:
|
93
|
+
This software is not feature-complete and in pre-release testing.
|
94
|
+
|
95
|
+
## AUTHOR
|
96
|
+
|
97
|
+
Markus Strauss <<log2mail@dev.sieb.mx>>
|
98
|
+
|
99
|
+
## THANKS
|
100
|
+
|
101
|
+
Many thanks to Michael Krax for writing the classic **log2mail** in the first place.
|
102
|
+
|
103
|
+
## SEE ALSO
|
104
|
+
|
105
|
+
Documentation for the classic log2mail software by Michael Krax:
|
106
|
+
|
107
|
+
* log2mail(8), log2mail.conf(5)
|
108
|
+
* [Configuration notice from the Debian project]( https://raw.githubusercontent.com/lordlamer/log2mail/e6beb36644ce74639cbc453e664a08ed15f138b9/Configuration)
|
109
|
+
|
110
|
+
|
111
|
+
[SYNOPSIS]: #SYNOPSIS "SYNOPSIS"
|
112
|
+
[DESCRIPTION]: #DESCRIPTION "DESCRIPTION"
|
113
|
+
[OPTIONS]: #OPTIONS "OPTIONS"
|
114
|
+
[ENVIRONMENT]: #ENVIRONMENT "ENVIRONMENT"
|
115
|
+
[SECURITY CONSIDERATIONS]: #SECURITY-CONSIDERATIONS "SECURITY CONSIDERATIONS"
|
116
|
+
[BUGS]: #BUGS "BUGS"
|
117
|
+
[HISTORY]: #HISTORY "HISTORY"
|
118
|
+
[AUTHOR]: #AUTHOR "AUTHOR"
|
119
|
+
[THANKS]: #THANKS "THANKS"
|
120
|
+
[SEE ALSO]: #SEE-ALSO "SEE ALSO"
|
121
|
+
|
122
|
+
|
123
|
+
[log2mail.rb(1)]: log2mail.1.html
|
data/Rakefile
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
require 'bundler/gem_tasks'
|
2
|
+
|
3
|
+
require 'rspec/core/rake_task'
|
4
|
+
RSpec::Core::RakeTask.new('spec')
|
5
|
+
|
6
|
+
require 'cucumber/rake/task'
|
7
|
+
Cucumber::Rake::Task.new(:features) do |t|
|
8
|
+
t.cucumber_opts = "features --format pretty"
|
9
|
+
end
|
10
|
+
|
11
|
+
require 'rake/notes/rake_task'
|
12
|
+
|
13
|
+
task :default => [:spec, :features, :man]
|
14
|
+
|
15
|
+
desc 'Build the manual'
|
16
|
+
task :man do
|
17
|
+
require 'ronn'
|
18
|
+
sh "ronn -w -s toc -r5 --markdown man/*.ronn"
|
19
|
+
FileUtils.mv("man/log2mail.1.markdown", "README.md")
|
20
|
+
end
|
data/bin/log2mail.rb
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Feature: Configuration Examination
|
2
|
+
In order verify correct configuration
|
3
|
+
As a respnosible system administrator
|
4
|
+
I want to see a configuration summary
|
5
|
+
|
6
|
+
Scenario: Show configuration settings
|
7
|
+
Given I use mode "configtest"
|
8
|
+
And config "config_1"
|
9
|
+
When log2mail is run
|
10
|
+
Then the output should be:
|
11
|
+
| File | Pattern | Recipient | Settings |
|
12
|
+
| test.log | string pattern | recipient@test.itstrauss.eu | |
|
13
|
+
| test.log | /regexp pattern/ | recipient@test.itstrauss.eu | |
|
14
|
+
Scenario: Show effective configuration settings
|
15
|
+
Given I use mode "configtest"
|
16
|
+
And config "config_1"
|
17
|
+
And parameter "-e"
|
18
|
+
When log2mail is run
|
19
|
+
Then the output should be:
|
20
|
+
| File | Pattern | Recipient | Effective Settings |
|
21
|
+
| test.log | string pattern | recipient@test.itstrauss.eu | fromaddr=log2mail |
|
22
|
+
| test.log | /regexp pattern/ | recipient@test.itstrauss.eu | fromaddr=log2mail |
|
@@ -0,0 +1,41 @@
|
|
1
|
+
def parse_configtest_output( txt )
|
2
|
+
rows = []; keys = nil
|
3
|
+
first_line = true
|
4
|
+
txt.each_line do |line|
|
5
|
+
next if line =~ /[+-]+/
|
6
|
+
if first_line
|
7
|
+
keys = line.split('|').map(&:strip) - ['|', '']
|
8
|
+
first_line = false
|
9
|
+
else
|
10
|
+
values = line.split('|').map(&:strip) - ['|', '']
|
11
|
+
rows << Hash[keys.zip(values)]
|
12
|
+
end
|
13
|
+
end
|
14
|
+
rows
|
15
|
+
end
|
16
|
+
|
17
|
+
Given(/^I use mode "(.*?)"$/) do |mode|
|
18
|
+
@mode = mode
|
19
|
+
end
|
20
|
+
|
21
|
+
Given(/^config "(.*?)"$/) do |config|
|
22
|
+
@config = config
|
23
|
+
end
|
24
|
+
|
25
|
+
Given(/^parameter "(.*?)"$/) do |args|
|
26
|
+
@args = args
|
27
|
+
end
|
28
|
+
|
29
|
+
When(/^log(\d+)mail is run$/) do |arg1|
|
30
|
+
@output = `bundle exec bin/log2mail.rb #{@mode} --config features/log2mail_configurations/#{@config} #{@args}`
|
31
|
+
# puts @output.inspect
|
32
|
+
# puts parse_configtest_output(@output).inspect
|
33
|
+
end
|
34
|
+
|
35
|
+
Then(/^the output should be:$/) do |table|
|
36
|
+
# table is a Cucumber::Ast::Table
|
37
|
+
table.map_headers!
|
38
|
+
table.map_column!(:Settings) { |cell| cell.empty? ? nil : cell } \
|
39
|
+
if table.hashes.first[:Settings]
|
40
|
+
table.diff! parse_configtest_output(@output), :surplus_col => true
|
41
|
+
end
|
data/lib/ext/kernel.rb
ADDED
data/lib/ext/main.rb
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
module Main
|
2
|
+
class Program
|
3
|
+
module InstanceMethods
|
4
|
+
|
5
|
+
def logger= log
|
6
|
+
unless(defined?(@logger) and @logger == log)
|
7
|
+
case log
|
8
|
+
when ::Logger, Logger, Syslog::Logger
|
9
|
+
@logger = log
|
10
|
+
else
|
11
|
+
if log.is_a?(Array)
|
12
|
+
@logger = Logger.new(*log)
|
13
|
+
else
|
14
|
+
@logger = Logger.new(log)
|
15
|
+
@logger.level = logger_level
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
@logger
|
20
|
+
end
|
21
|
+
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
data/lib/ext/string.rb
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
class String
|
2
|
+
# based on https://www.ruby-forum.com/topic/193809#844629
|
3
|
+
def to_r
|
4
|
+
if self.strip.match(/\A\/(.*)\/(.*)\Z/mx)
|
5
|
+
regexp, flags = $1, $2
|
6
|
+
fail "Not a valid regular expression. Valid flags in (/regexp/flags) are x, i and m" \
|
7
|
+
if !regexp || flags =~ /[^xim]/m
|
8
|
+
|
9
|
+
x = /x/.match(flags) && Regexp::EXTENDED
|
10
|
+
i = /i/.match(flags) && Regexp::IGNORECASE
|
11
|
+
m = /m/.match(flags) && Regexp::MULTILINE
|
12
|
+
|
13
|
+
rxp = Regexp.new regexp , [x,i,m].inject(0){|a,f| f ? a+f : a }
|
14
|
+
def rxp.from_string?; false end
|
15
|
+
rxp
|
16
|
+
else
|
17
|
+
# build regexp from regular string
|
18
|
+
rxp = Regexp.new( self )
|
19
|
+
def rxp.from_string?; true end
|
20
|
+
rxp
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,208 @@
|
|
1
|
+
require 'syslog'
|
2
|
+
require 'logger'
|
3
|
+
|
4
|
+
##
|
5
|
+
# Syslog::Logger is a Logger work-alike that logs via syslog instead of to a
|
6
|
+
# file. You can use Syslog::Logger to aggregate logs between multiple
|
7
|
+
# machines.
|
8
|
+
#
|
9
|
+
# By default, Syslog::Logger uses the program name 'ruby', but this can be
|
10
|
+
# changed via the first argument to Syslog::Logger.new.
|
11
|
+
#
|
12
|
+
# NOTE! You can only set the Syslog::Logger program name when you initialize
|
13
|
+
# Syslog::Logger for the first time. This is a limitation of the way
|
14
|
+
# Syslog::Logger uses syslog (and in some ways, a limitation of the way
|
15
|
+
# syslog(3) works). Attempts to change Syslog::Logger's program name after
|
16
|
+
# the first initialization will be ignored.
|
17
|
+
#
|
18
|
+
# === Example
|
19
|
+
#
|
20
|
+
# The following will log to syslogd on your local machine:
|
21
|
+
#
|
22
|
+
# require 'syslog/logger'
|
23
|
+
#
|
24
|
+
# log = Syslog::Logger.new 'my_program'
|
25
|
+
# log.info 'this line will be logged via syslog(3)'
|
26
|
+
#
|
27
|
+
# Also the facility may be set to specify the facility level which will be used:
|
28
|
+
#
|
29
|
+
# log.info 'this line will be logged using Syslog default facility level'
|
30
|
+
#
|
31
|
+
# log_local1 = Syslog::Logger.new 'my_program', Syslog::LOG_LOCAL1
|
32
|
+
# log_local1.info 'this line will be logged using local1 facility level'
|
33
|
+
#
|
34
|
+
#
|
35
|
+
# You may need to perform some syslog.conf setup first. For a BSD machine add
|
36
|
+
# the following lines to /etc/syslog.conf:
|
37
|
+
#
|
38
|
+
# !my_program
|
39
|
+
# *.* /var/log/my_program.log
|
40
|
+
#
|
41
|
+
# Then touch /var/log/my_program.log and signal syslogd with a HUP
|
42
|
+
# (killall -HUP syslogd, on FreeBSD).
|
43
|
+
#
|
44
|
+
# If you wish to have logs automatically roll over and archive, see the
|
45
|
+
# newsyslog.conf(5) and newsyslog(8) man pages.
|
46
|
+
|
47
|
+
class Syslog::Logger
|
48
|
+
# Default formatter for log messages.
|
49
|
+
class Formatter
|
50
|
+
def call severity, time, progname, msg
|
51
|
+
clean msg
|
52
|
+
end
|
53
|
+
|
54
|
+
private
|
55
|
+
|
56
|
+
##
|
57
|
+
# Clean up messages so they're nice and pretty.
|
58
|
+
|
59
|
+
def clean message
|
60
|
+
message = message.to_s.strip
|
61
|
+
message.gsub!(/\e\[[0-9;]*m/, '') # remove useless ansi color codes
|
62
|
+
return message
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
##
|
67
|
+
# The version of Syslog::Logger you are using.
|
68
|
+
|
69
|
+
VERSION = '2.1.0'
|
70
|
+
|
71
|
+
##
|
72
|
+
# Maps Logger warning types to syslog(3) warning types.
|
73
|
+
#
|
74
|
+
# Messages from Ruby applications are not considered as critical as messages
|
75
|
+
# from other system daemons using syslog(3), so most messages are reduced by
|
76
|
+
# one level. For example, a fatal message for Ruby's Logger is considered
|
77
|
+
# an error for syslog(3).
|
78
|
+
|
79
|
+
LEVEL_MAP = {
|
80
|
+
::Logger::UNKNOWN => Syslog::LOG_ALERT,
|
81
|
+
::Logger::FATAL => Syslog::LOG_ERR,
|
82
|
+
::Logger::ERROR => Syslog::LOG_WARNING,
|
83
|
+
::Logger::WARN => Syslog::LOG_NOTICE,
|
84
|
+
::Logger::INFO => Syslog::LOG_INFO,
|
85
|
+
::Logger::DEBUG => Syslog::LOG_DEBUG,
|
86
|
+
}
|
87
|
+
|
88
|
+
##
|
89
|
+
# Returns the internal Syslog object that is initialized when the
|
90
|
+
# first instance is created.
|
91
|
+
|
92
|
+
def self.syslog
|
93
|
+
@@syslog
|
94
|
+
end
|
95
|
+
|
96
|
+
##
|
97
|
+
# Specifies the internal Syslog object to be used.
|
98
|
+
|
99
|
+
def self.syslog= syslog
|
100
|
+
@@syslog = syslog
|
101
|
+
end
|
102
|
+
|
103
|
+
##
|
104
|
+
# Builds a methods for level +meth+.
|
105
|
+
|
106
|
+
def self.make_methods meth
|
107
|
+
level = ::Logger.const_get(meth.upcase)
|
108
|
+
eval <<-EOM, nil, __FILE__, __LINE__ + 1
|
109
|
+
def #{meth}(message = nil, &block)
|
110
|
+
add(#{level}, message, &block)
|
111
|
+
end
|
112
|
+
|
113
|
+
def #{meth}?
|
114
|
+
@level <= #{level}
|
115
|
+
end
|
116
|
+
EOM
|
117
|
+
end
|
118
|
+
|
119
|
+
##
|
120
|
+
# :method: unknown
|
121
|
+
#
|
122
|
+
# Logs a +message+ at the unknown (syslog alert) log level, or logs the
|
123
|
+
# message returned from the block.
|
124
|
+
|
125
|
+
##
|
126
|
+
# :method: fatal
|
127
|
+
#
|
128
|
+
# Logs a +message+ at the fatal (syslog err) log level, or logs the message
|
129
|
+
# returned from the block.
|
130
|
+
|
131
|
+
##
|
132
|
+
# :method: error
|
133
|
+
#
|
134
|
+
# Logs a +message+ at the error (syslog warning) log level, or logs the
|
135
|
+
# message returned from the block.
|
136
|
+
|
137
|
+
##
|
138
|
+
# :method: warn
|
139
|
+
#
|
140
|
+
# Logs a +message+ at the warn (syslog notice) log level, or logs the
|
141
|
+
# message returned from the block.
|
142
|
+
|
143
|
+
##
|
144
|
+
# :method: info
|
145
|
+
#
|
146
|
+
# Logs a +message+ at the info (syslog info) log level, or logs the message
|
147
|
+
# returned from the block.
|
148
|
+
|
149
|
+
##
|
150
|
+
# :method: debug
|
151
|
+
#
|
152
|
+
# Logs a +message+ at the debug (syslog debug) log level, or logs the
|
153
|
+
# message returned from the block.
|
154
|
+
|
155
|
+
Logger::Severity::constants.each do |severity|
|
156
|
+
make_methods severity.downcase
|
157
|
+
end
|
158
|
+
|
159
|
+
##
|
160
|
+
# Log level for Logger compatibility.
|
161
|
+
|
162
|
+
attr_accessor :level
|
163
|
+
|
164
|
+
# Logging formatter, as a +Proc+ that will take four arguments and
|
165
|
+
# return the formatted message. The arguments are:
|
166
|
+
#
|
167
|
+
# +severity+:: The Severity of the log message.
|
168
|
+
# +time+:: A Time instance representing when the message was logged.
|
169
|
+
# +progname+:: The #progname configured, or passed to the logger method.
|
170
|
+
# +msg+:: The _Object_ the user passed to the log message; not necessarily a
|
171
|
+
# String.
|
172
|
+
#
|
173
|
+
# The block should return an Object that can be written to the logging
|
174
|
+
# device via +write+. The default formatter is used when no formatter is
|
175
|
+
# set.
|
176
|
+
attr_accessor :formatter
|
177
|
+
|
178
|
+
##
|
179
|
+
# The facility argument is used to specify what type of program is logging the message.
|
180
|
+
|
181
|
+
attr_accessor :facility
|
182
|
+
|
183
|
+
##
|
184
|
+
# Fills in variables for Logger compatibility. If this is the first
|
185
|
+
# instance of Syslog::Logger, +program_name+ may be set to change the logged
|
186
|
+
# program name. The +facility+ may be set to specify the facility level which will be used.
|
187
|
+
#
|
188
|
+
# Due to the way syslog works, only one program name may be chosen.
|
189
|
+
|
190
|
+
def initialize program_name = 'ruby', facility = nil
|
191
|
+
@level = ::Logger::DEBUG
|
192
|
+
@formatter = Formatter.new
|
193
|
+
|
194
|
+
@@syslog ||= Syslog.open(program_name)
|
195
|
+
|
196
|
+
@facility = (facility || @@syslog.facility)
|
197
|
+
end
|
198
|
+
|
199
|
+
##
|
200
|
+
# Almost duplicates Logger#add. +progname+ is ignored.
|
201
|
+
|
202
|
+
def add severity, message = nil, progname = nil, &block
|
203
|
+
severity ||= ::Logger::UNKNOWN
|
204
|
+
@level <= severity and
|
205
|
+
@@syslog.log( (LEVEL_MAP[severity] | @facility), '%s', formatter.call(severity, Time.now, progname, (message || block.call)) )
|
206
|
+
true
|
207
|
+
end
|
208
|
+
end
|