cbrunnkvist-psd_logger 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
data/.gitignore ADDED
@@ -0,0 +1,21 @@
1
+ ## MAC OS
2
+ .DS_Store
3
+
4
+ ## TEXTMATE
5
+ *.tmproj
6
+ tmtags
7
+
8
+ ## EMACS
9
+ *~
10
+ \#*
11
+ .\#*
12
+
13
+ ## VIM
14
+ *.swp
15
+
16
+ ## PROJECT::GENERAL
17
+ coverage
18
+ rdoc
19
+ pkg
20
+
21
+ ## PROJECT::SPECIFIC
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2010 Conny Brunnkvist
2
+
3
+ includes portions (c) 2009 Sean Cashin
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.rdoc ADDED
@@ -0,0 +1,3 @@
1
+ = psd_logger
2
+
3
+ An improved Logger replacement that logs to syslog, with custom tweaks added & defaults set. For a generic one please see http://github.com/cpowell/sysloglogger
data/Rakefile ADDED
@@ -0,0 +1,54 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "cbrunnkvist-psd_logger"
8
+ gem.summary = %Q{A Sanger/PSD specific fork of the SyslogLogger gem.}
9
+ gem.description = %Q{An improved Logger replacement that logs to syslog, with custom tweaks added & defaults set. For a generic one please see http://github.com/cpowell/sysloglogger}
10
+ gem.email = "cb16@sanger.ac.uk"
11
+ gem.homepage = "http://github.com/cbrunnkvist/psd_logger"
12
+ gem.authors = ["Conny Brunnkvist"]
13
+ gem.add_development_dependency "shoulda"
14
+ gem.add_development_dependency "mocha"
15
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
16
+ end
17
+ Jeweler::GemcutterTasks.new
18
+ rescue LoadError
19
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
20
+ end
21
+
22
+ require 'rake/testtask'
23
+ Rake::TestTask.new(:test) do |test|
24
+ test.libs << 'lib' << 'test'
25
+ test.pattern = 'test/**/test_*.rb'
26
+ test.verbose = true
27
+ end
28
+
29
+ begin
30
+ require 'rcov/rcovtask'
31
+ Rcov::RcovTask.new do |test|
32
+ test.libs << 'test'
33
+ test.pattern = 'test/**/test_*.rb'
34
+ test.verbose = true
35
+ end
36
+ rescue LoadError
37
+ task :rcov do
38
+ abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
39
+ end
40
+ end
41
+
42
+ task :test => :check_dependencies
43
+
44
+ task :default => :test
45
+
46
+ require 'rake/rdoctask'
47
+ Rake::RDocTask.new do |rdoc|
48
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
49
+
50
+ rdoc.rdoc_dir = 'rdoc'
51
+ rdoc.title = "syslog_logger #{version}"
52
+ rdoc.rdoc_files.include('README*')
53
+ rdoc.rdoc_files.include('lib/**/*.rb')
54
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.1
data/bin/psd_logger ADDED
@@ -0,0 +1,27 @@
1
+ #!/usr/bin/env ruby
2
+ require 'getoptlong'
3
+ require 'psd_logger'
4
+
5
+ opts = GetoptLong.new(
6
+ [ '--tag', '-t', GetoptLong::OPTIONAL_ARGUMENT ],
7
+ [ '--prefix', '-p', GetoptLong::OPTIONAL_ARGUMENT ]
8
+ )
9
+
10
+ tag = 'logger'
11
+ opts.each do |opt_name,opt_value|
12
+ if (opt_name == '--tag') && (opt_value != '')
13
+ tag = opt_value
14
+ end
15
+ end
16
+ psd_logger = PsdLogger.new(tag)
17
+
18
+ if ARGV.length == 0
19
+ begin
20
+ while line = STDIN.readline
21
+ psd_logger.error(line)
22
+ end
23
+ rescue EOFError
24
+ end
25
+ else
26
+ psd_logger.error(msg)
27
+ end
@@ -0,0 +1,61 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{cbrunnkvist-psd_logger}
8
+ s.version = "0.1.1"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Conny Brunnkvist"]
12
+ s.date = %q{2010-09-06}
13
+ s.default_executable = %q{psd_logger}
14
+ s.description = %q{An improved Logger replacement that logs to syslog, with custom tweaks added & defaults set. For a generic one please see http://github.com/cpowell/sysloglogger}
15
+ s.email = %q{cb16@sanger.ac.uk}
16
+ s.executables = ["psd_logger"]
17
+ s.extra_rdoc_files = [
18
+ "LICENSE",
19
+ "README.rdoc"
20
+ ]
21
+ s.files = [
22
+ ".document",
23
+ ".gitignore",
24
+ "LICENSE",
25
+ "README.rdoc",
26
+ "Rakefile",
27
+ "VERSION",
28
+ "bin/psd_logger",
29
+ "cbrunnkvist-psd_logger.gemspec",
30
+ "lib/psd_logger.rb",
31
+ "lib/psd_logger_env_formatting.rb",
32
+ "test/helper.rb",
33
+ "test/test_syslog_logger.rb"
34
+ ]
35
+ s.homepage = %q{http://github.com/cbrunnkvist/psd_logger}
36
+ s.rdoc_options = ["--charset=UTF-8"]
37
+ s.require_paths = ["lib"]
38
+ s.rubygems_version = %q{1.3.7}
39
+ s.summary = %q{A Sanger/PSD specific fork of the SyslogLogger gem.}
40
+ s.test_files = [
41
+ "test/helper.rb",
42
+ "test/test_syslog_logger.rb"
43
+ ]
44
+
45
+ if s.respond_to? :specification_version then
46
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
47
+ s.specification_version = 3
48
+
49
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
50
+ s.add_development_dependency(%q<shoulda>, [">= 0"])
51
+ s.add_development_dependency(%q<mocha>, [">= 0"])
52
+ else
53
+ s.add_dependency(%q<shoulda>, [">= 0"])
54
+ s.add_dependency(%q<mocha>, [">= 0"])
55
+ end
56
+ else
57
+ s.add_dependency(%q<shoulda>, [">= 0"])
58
+ s.add_dependency(%q<mocha>, [">= 0"])
59
+ end
60
+ end
61
+
data/lib/psd_logger.rb ADDED
@@ -0,0 +1,142 @@
1
+ require 'syslog'
2
+ require 'logger'
3
+
4
+ ##
5
+ # PsdLogger includes code from SeattleRb's SyslogLogger
6
+ #
7
+ # = Sample usage with Rails
8
+ #
9
+ # == config/environment/production.rb
10
+ #
11
+ # Add the following lines:
12
+ #
13
+ # require 'psd_logger'
14
+ # RAILS_DEFAULT_LOGGER = PsdLogger.new
15
+
16
+ class PsdLogger
17
+ ##
18
+ # The version of SyslogLogger you are using.
19
+ VERSION = '0.0.1'
20
+ # From 'man syslog.h':
21
+ # LOG_EMERG A panic condition was reported to all processes.
22
+ # LOG_ALERT A condition that should be corrected immediately.
23
+ # LOG_CRIT A critical condition.
24
+ # LOG_ERR An error message.
25
+ # LOG_WARNING A warning message.
26
+ # LOG_NOTICE A condition requiring special handling.
27
+ # LOG_INFO A general information message.
28
+ # LOG_DEBUG A message useful for debugging programs.
29
+
30
+ # From logger rdoc:
31
+ # FATAL: an unhandleable error that results in a program crash
32
+ # ERROR: a handleable error condition
33
+ # WARN: a warning
34
+ # INFO: generic (useful) information about system operation
35
+ # DEBUG: low-level information for developers
36
+
37
+ ##
38
+ # Maps Logger warning types to syslog(3) warning types.
39
+ LOGGER_MAP = {
40
+ :unknown => :alert,
41
+ :fatal => :alert,
42
+ :error => :err,
43
+ :warn => :warning,
44
+ :info => :info,
45
+ :debug => :debug
46
+ }
47
+
48
+ # Maps Logger log levels to their values so we can silence.
49
+ LOGGER_LEVEL_MAP = {}
50
+
51
+ LOGGER_MAP.each_key do |key|
52
+ LOGGER_LEVEL_MAP[key] = Logger.const_get key.to_s.upcase
53
+ end
54
+ include Logger::Severity
55
+ ##
56
+ # Maps Logger log level values to syslog log levels.
57
+ LEVEL_LOGGER_MAP = {}
58
+
59
+ LOGGER_LEVEL_MAP.invert.each do |level, severity|
60
+ LEVEL_LOGGER_MAP[level] = LOGGER_MAP[severity]
61
+ end
62
+
63
+ # Builds a methods for level +meth+.
64
+ for severity in Logger::Severity.constants
65
+ class_eval <<-EOT, __FILE__, __LINE__
66
+ def #{severity.downcase}(message = nil, progname = nil, &block) # def debug(message = nil, progname = nil, &block)
67
+ add(#{severity}, message, progname, &block) # add(DEBUG, message, progname, &block)
68
+ end # end
69
+ #
70
+ def #{severity.downcase}? # def debug?
71
+ #{severity} >= @level # DEBUG >= @level
72
+ end # end
73
+ EOT
74
+ end
75
+
76
+ # Log level for Logger compatibility.
77
+ attr_accessor :level, :filter
78
+
79
+ ##
80
+ # Fills in variables for Logger compatibility. If this is the first
81
+ # instance of SyslogLogger, +program_name+ may be set to change the logged
82
+ # program name and +facility+ may be set to specify a custom facility
83
+ # with your syslog daemon.
84
+ #
85
+ # Due to the way syslog works, program name may be set once only (needs to reopen otherwise).
86
+ def initialize(tag_suffix = '')
87
+ return if defined? SYSLOG
88
+
89
+ if tag_suffix == ''
90
+ tag_suffix = 'logger'
91
+ end
92
+
93
+ @level = PsdLogger::INFO
94
+ self.class.const_set :SYSLOG, Syslog.open("psd_#{tag_suffix}")
95
+ self.debug("PsdLogger.initialize()")
96
+ end
97
+
98
+ ##
99
+ # Almost duplicates Logger#add. +progname+ is prepended to the beginning of a message.
100
+ def add(severity, message = nil, progname = nil, &block)
101
+ severity ||= Logger::UNKNOWN
102
+ if severity >= @level
103
+ prepend = progname ? "[#{progname}] " : nil
104
+ prepend ||= @filter ? "[#{@filter}] " : ''
105
+ message = clean(message || block.call)
106
+ SYSLOG.send LEVEL_LOGGER_MAP[severity], prepend + clean(message)
107
+ end
108
+ true
109
+ end
110
+
111
+ ##
112
+ # Allows messages of a particular log level to be ignored temporarily.
113
+ def silence(temporary_level = Logger::ERROR)
114
+ old_logger_level = @level
115
+ @level = temporary_level
116
+ yield
117
+ ensure
118
+ @level = old_logger_level
119
+ end
120
+
121
+ # In Logger, this dumps the raw message; the closest equivalent
122
+ # would be Logger::UNKNOWN
123
+ def <<(message)
124
+ add(Logger::UNKNOWN, message)
125
+ end
126
+
127
+ private
128
+
129
+ ##
130
+ # Clean up messages so they're nice and pretty.
131
+ def clean(message)
132
+ message = message.to_s.dup
133
+ message.strip!
134
+ message.gsub!(/%/, '%%') # syslog(3) freaks on % (printf)
135
+ message.gsub!(/\e\[[^m]*m/, '') # remove useless ansi color codes
136
+ if defined?(Rails)
137
+ message = "#{Rails.env}/#{Rails.root.split.last} #{message}"
138
+ end
139
+ return message
140
+ end
141
+
142
+ end
@@ -0,0 +1,38 @@
1
+ # Adds some default information to syslog messages.
2
+ # If you pass a User or Device object as the first parameter, it will log that user/device's id
3
+ # If you pass a block from a controller, it will automatically log the current user id
4
+ # If you pass a block, the class name of the block's context will be added
5
+ #
6
+ # Examples
7
+ # logger.debug "O'Doyle Rules!"
8
+ # #=> [development] [DEBUG: 2008-01-25 14:16:12.12345] O'Doyle Rules!
9
+ #
10
+ # #from within a controller...
11
+ # logger.error {"Something is messed up!"}
12
+ # #=> [development] [ERROR: 2008-01-25 14:16:12.12345] [123] [ClassName] Something is messed up!
13
+
14
+ class SyslogLogger
15
+
16
+ # short names for "DEBUG", "INFO", ...
17
+ # must be ordered to correspond to severity constants defined in
18
+ # ActiveSupport::BufferedLogger::Severity
19
+ #@@log_level_names = %w( D I W E F U )
20
+ #LOG_NAME_FIELD_WIDTH = 1
21
+
22
+ @@log_level_names = %w( DEBUG INFO WARN ERROR FATAL UNKNOWN )
23
+ LOG_NAME_FIELD_WIDTH = 7
24
+
25
+ def add_with_formatting(severity, message = nil, progname = nil, &block)
26
+ severity ||= Logger::UNKNOWN
27
+ message = "[#{RAILS_ENV}] [#{@@log_level_names[severity].ljust(LOG_NAME_FIELD_WIDTH)}: #{time.strftime("%Y-%m-%d %H:%M:%S")}.#{time.usec.to_s.rjust(6, '0')}] #{message}"
28
+
29
+ if(block)
30
+ add_without_formatting(severity, message, progname,
31
+ &Proc.new{g_log_formatter(severity, nil, user, &block)})
32
+ else
33
+ add_without_formatting(severity, message, progname)
34
+ end
35
+ end
36
+ alias_method_chain :add, :formatting
37
+
38
+ end
data/test/helper.rb ADDED
@@ -0,0 +1,11 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'shoulda'
4
+ require 'mocha'
5
+
6
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
7
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
8
+ require 'syslog_logger'
9
+
10
+ class Test::Unit::TestCase
11
+ end
@@ -0,0 +1,25 @@
1
+ require 'helper'
2
+
3
+ class TestSyslogLogger < Test::Unit::TestCase
4
+ context "on first initialization of default SyslogLogger object" do
5
+ setup do
6
+ @logger = SyslogLogger.new
7
+ end
8
+ should "have level set to DEBUG" do
9
+ assert_equal @logger.level, Logger::DEBUG
10
+ end
11
+ end
12
+ context "a SyslogLogger object" do
13
+ setup do
14
+ @logger = SyslogLogger.new
15
+ end
16
+ Logger::Severity.constants.each do |severity|
17
+ should "respond to method #{severity}" do
18
+ @logger.send(severity.downcase.to_sym, 'message', 'progname')
19
+ end
20
+ should "respond to method #{severity}?" do
21
+ @logger.send("#{severity.downcase}?".to_sym)
22
+ end
23
+ end
24
+ end
25
+ end
metadata ADDED
@@ -0,0 +1,107 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cbrunnkvist-psd_logger
3
+ version: !ruby/object:Gem::Version
4
+ hash: 25
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 1
10
+ version: 0.1.1
11
+ platform: ruby
12
+ authors:
13
+ - Conny Brunnkvist
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-09-06 00:00:00 +01:00
19
+ default_executable: psd_logger
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: shoulda
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 3
30
+ segments:
31
+ - 0
32
+ version: "0"
33
+ type: :development
34
+ version_requirements: *id001
35
+ - !ruby/object:Gem::Dependency
36
+ name: mocha
37
+ prerelease: false
38
+ requirement: &id002 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ hash: 3
44
+ segments:
45
+ - 0
46
+ version: "0"
47
+ type: :development
48
+ version_requirements: *id002
49
+ description: An improved Logger replacement that logs to syslog, with custom tweaks added & defaults set. For a generic one please see http://github.com/cpowell/sysloglogger
50
+ email: cb16@sanger.ac.uk
51
+ executables:
52
+ - psd_logger
53
+ extensions: []
54
+
55
+ extra_rdoc_files:
56
+ - LICENSE
57
+ - README.rdoc
58
+ files:
59
+ - .document
60
+ - .gitignore
61
+ - LICENSE
62
+ - README.rdoc
63
+ - Rakefile
64
+ - VERSION
65
+ - bin/psd_logger
66
+ - cbrunnkvist-psd_logger.gemspec
67
+ - lib/psd_logger.rb
68
+ - lib/psd_logger_env_formatting.rb
69
+ - test/helper.rb
70
+ - test/test_syslog_logger.rb
71
+ has_rdoc: true
72
+ homepage: http://github.com/cbrunnkvist/psd_logger
73
+ licenses: []
74
+
75
+ post_install_message:
76
+ rdoc_options:
77
+ - --charset=UTF-8
78
+ require_paths:
79
+ - lib
80
+ required_ruby_version: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ">="
84
+ - !ruby/object:Gem::Version
85
+ hash: 3
86
+ segments:
87
+ - 0
88
+ version: "0"
89
+ required_rubygems_version: !ruby/object:Gem::Requirement
90
+ none: false
91
+ requirements:
92
+ - - ">="
93
+ - !ruby/object:Gem::Version
94
+ hash: 3
95
+ segments:
96
+ - 0
97
+ version: "0"
98
+ requirements: []
99
+
100
+ rubyforge_project:
101
+ rubygems_version: 1.3.7
102
+ signing_key:
103
+ specification_version: 3
104
+ summary: A Sanger/PSD specific fork of the SyslogLogger gem.
105
+ test_files:
106
+ - test/helper.rb
107
+ - test/test_syslog_logger.rb