logit 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (6) hide show
  1. data/Manifest +4 -0
  2. data/README.rdoc +88 -0
  3. data/Rakefile +13 -0
  4. data/lib/logit.rb +133 -0
  5. data/logit.gemspec +30 -0
  6. metadata +78 -0
data/Manifest ADDED
@@ -0,0 +1,4 @@
1
+ README.rdoc
2
+ Rakefile
3
+ lib/logit.rb
4
+ Manifest
data/README.rdoc ADDED
@@ -0,0 +1,88 @@
1
+ = LogIt
2
+ LogIt makes it easy to add custom logging to your Ruby or Rails
3
+ application and has sensible formatting out of the box.
4
+
5
+
6
+ = Usage
7
+
8
+ === Minimal configuration
9
+
10
+ LogIt adds a class method to any including class to setup a logger method.
11
+ For example:
12
+
13
+ require 'logit'
14
+
15
+ class Publisher
16
+ include Logit
17
+
18
+ logs_to :publisher
19
+
20
+ def publish
21
+ logger.info("doing publish")
22
+ # do stuff...
23
+ end
24
+ end
25
+
26
+ This will write logs to a publisher.log file in the current directory.
27
+
28
+ === More config options
29
+
30
+ ==== Writing to a specific directory
31
+
32
+ logs_to '/var/log/publishing/publisher.log'
33
+
34
+ ==== Adding a progname to log entries
35
+
36
+ logs_to :publisher, :progname => "Publisher #{Process.pid}"
37
+
38
+ this will add something like 'Publisher 1234' to log entries.
39
+
40
+ ==== Configuring log rotation
41
+
42
+ logs_to :publisher, :shift_age => 5, :shift_size => 102400
43
+
44
+ this will rotate logs up to a total of 5 files with max size of 102400
45
+ bytes.
46
+
47
+
48
+ === Running in Rails
49
+
50
+ LogIt automatically detects if you're running in a Rails environment. If
51
+ so, it will write to the Rails log directory and appends the
52
+ environment to the log file name. For example:
53
+
54
+ RAILS_ROOT/log/publisher_development.log
55
+
56
+ = Example log
57
+
58
+ 09-12-2010 10:27:10 INFO [publisher 4607]: Publishing files to endpoint.
59
+ 09-12-2010 10:27:15 INFO [publisher 4607]: Publishing completed.
60
+ 09-12-2010 10:27:56 INFO [publisher 4621]: Publishing files to endpoint.
61
+ 09-12-2010 10:28:01 ERROR [publisher 4621]: An exception occurred while publishing files: Connection reset by peer.
62
+ 09-12-2010 10:28:32 INFO [publisher 4634]: Publishing files to endpoint.
63
+ 09-12-2010 10:28:32 WARN [publisher 4634]: No files available to publish.
64
+
65
+ = Installation
66
+
67
+ == As a Gem
68
+
69
+ $ gem install logit
70
+
71
+ == As a Rails plugin
72
+
73
+ $ script/plugin install git://github.com/ssayles/logit.git
74
+
75
+
76
+ = TODO
77
+
78
+ * Allow users to pass in a proc or a name of a method to call to handle formatting of log entries.
79
+ * Add some tests?
80
+
81
+
82
+ = Credits
83
+
84
+ LogIt is written and maintained by {Scott Sayles}[mailto:ssayles@users.sourceforge.net].
85
+
86
+ = Copyright
87
+
88
+ Copyright (c) 2010 Scott Sayles. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,13 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+ require 'echoe'
4
+
5
+ Echoe.new('logit', '1.0.0') do |p|
6
+ p.description = 'Easily add custom logging abilities.'
7
+ p.url = 'http://github.com/codemariner/logit'
8
+ p.author = 'Scott Sayles'
9
+ p.email = 'ssayles@users.sourceforge.net'
10
+ p.development_dependencies = []
11
+ end
12
+
13
+ #Dir["#{File.dirname(__FILE__)}/tasks/*.rake"].sort.each {|ext| load ext}
data/lib/logit.rb ADDED
@@ -0,0 +1,133 @@
1
+ module Logit
2
+ def self.included(base)
3
+ base.extend ClassMethods
4
+ end
5
+
6
+ module ClassMethods
7
+ DEFAULT_OPTS = {:write_mode => 'a'}
8
+
9
+ begin
10
+ Module.const_get(:Logger)
11
+ rescue NameError
12
+ require 'logger'
13
+ end
14
+
15
+ # === Options
16
+ # * +:write_mode+ - mode used when opening the log file. You'll ususually just want 'a' for append or 'w' for overwrite. Defaults to 'a'.
17
+ # * +:shift_age+ - Number of old logs to keep or frequency of rotation.
18
+ # * +:shift_size+ - Maximum logfile size that only applies when <tt>:shift_age</tt> is a number.
19
+ # * +:progname+ - Logging program name. The <tt>:progname</tt> value is used in the default logging format if defined.
20
+ #
21
+ # === Examples
22
+ #
23
+ # class Publisher
24
+ # include Logit
25
+ #
26
+ # logs_to "/tmp/publisher.log"
27
+ #
28
+ # def do_it
29
+ # logger.info("doing something")
30
+ # end
31
+ # end
32
+ #
33
+ #
34
+ # class Publisher2
35
+ # include Logit
36
+ #
37
+ # logs_to :publisher, :progname => "Publisher #{Process.pid}"
38
+ # :shift_age => 'daily'
39
+ # def do_it
40
+ # logger.info("doing something")
41
+ # end
42
+ # end
43
+ #
44
+ def logs_to(name, opts={})
45
+ opts = DEFAULT_OPTS.merge(opts)
46
+ path = logit_log_name(name, opts)
47
+ self.send :define_method, :logger do
48
+ unless @logger
49
+ @logger = Logit::Logger.new(path, opts)
50
+ if opts[:progname]
51
+ @logger.progname = opts[:progname]
52
+ end
53
+ end
54
+ @logger
55
+ end
56
+ end
57
+
58
+
59
+ # Tries to figure out what the fully qualified path name
60
+ # of the log file should be.
61
+ #
62
+ def logit_log_name(name, opts)
63
+ path = name.to_s.strip
64
+
65
+ # if they are giving a path like '/var/log/foo.log'
66
+ # then we shouldn't presume to stick it in the Rails log dir
67
+ unless (path =~ /\/+/)
68
+ if (logit_in_rails?)
69
+ # take off any trailing .log so we can attach the environment
70
+ # name
71
+ path = logit_strip_dot_log(path)
72
+ path = File.join(RAILS_ROOT, 'log', "#{name}_#{Rails.env}.log")
73
+ end
74
+ end
75
+ # see if we need to append .log
76
+ # is this a bit presumptuous?
77
+ unless (path =~ /\.log$/)
78
+ path << ".log"
79
+ end
80
+ end
81
+
82
+ def logit_strip_dot_log(name)
83
+ if (name =~ /\.log$/)
84
+ name.slice(0, name =~ /\.log$/)
85
+ else
86
+ name
87
+ end
88
+ end
89
+ def logit_in_rails?
90
+ begin
91
+ Module.const_get(:Rails)
92
+ return true
93
+ rescue NameError
94
+ end
95
+ end
96
+ end
97
+
98
+
99
+
100
+ # The actual logger.
101
+ #
102
+ # Example:
103
+ #
104
+ # c = Logit::Logger.new('custom')
105
+ #
106
+ # such that the resulting log, for example, is
107
+ #
108
+ # #{RAILS_ROOT}/log/custom_development.log
109
+ #
110
+ # when running under Rails.
111
+ #
112
+ # You can flush the log by calling logger.flush().
113
+ #
114
+ class Logger < Logger
115
+
116
+ def initialize(log_path, opts = {:write_mode => 'a'})
117
+ @f = File.open(log_path, 'a')
118
+ super @f
119
+ end
120
+
121
+ def format_message(severity, timestamp, progname, msg)
122
+
123
+ name = (progname) ? " [#{progname}]" : ""
124
+
125
+ "#{timestamp.strftime('%d-%m-%Y %H:%M:%S')} #{severity.ljust(6)}#{name}: #{msg}\n"
126
+ end
127
+
128
+ def flush()
129
+ @f.flush()
130
+ end
131
+ end
132
+
133
+ end
data/logit.gemspec ADDED
@@ -0,0 +1,30 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = %q{logit}
5
+ s.version = "1.0.0"
6
+
7
+ s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
8
+ s.authors = ["Scott Sayles"]
9
+ s.date = %q{2010-12-09}
10
+ s.description = %q{Easily add custom logging abilities.}
11
+ s.email = %q{ssayles@users.sourceforge.net}
12
+ s.extra_rdoc_files = ["README.rdoc", "lib/logit.rb"]
13
+ s.files = ["README.rdoc", "Rakefile", "lib/logit.rb", "Manifest", "logit.gemspec"]
14
+ s.homepage = %q{http://github.com/codemariner/logit}
15
+ s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Logit", "--main", "README.rdoc"]
16
+ s.require_paths = ["lib"]
17
+ s.rubyforge_project = %q{logit}
18
+ s.rubygems_version = %q{1.3.7}
19
+ s.summary = %q{Easily add custom logging abilities.}
20
+
21
+ if s.respond_to? :specification_version then
22
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
23
+ s.specification_version = 3
24
+
25
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
26
+ else
27
+ end
28
+ else
29
+ end
30
+ end
metadata ADDED
@@ -0,0 +1,78 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: logit
3
+ version: !ruby/object:Gem::Version
4
+ hash: 23
5
+ prerelease: false
6
+ segments:
7
+ - 1
8
+ - 0
9
+ - 0
10
+ version: 1.0.0
11
+ platform: ruby
12
+ authors:
13
+ - Scott Sayles
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-12-09 00:00:00 -05:00
19
+ default_executable:
20
+ dependencies: []
21
+
22
+ description: Easily add custom logging abilities.
23
+ email: ssayles@users.sourceforge.net
24
+ executables: []
25
+
26
+ extensions: []
27
+
28
+ extra_rdoc_files:
29
+ - README.rdoc
30
+ - lib/logit.rb
31
+ files:
32
+ - README.rdoc
33
+ - Rakefile
34
+ - lib/logit.rb
35
+ - Manifest
36
+ - logit.gemspec
37
+ has_rdoc: true
38
+ homepage: http://github.com/codemariner/logit
39
+ licenses: []
40
+
41
+ post_install_message:
42
+ rdoc_options:
43
+ - --line-numbers
44
+ - --inline-source
45
+ - --title
46
+ - Logit
47
+ - --main
48
+ - README.rdoc
49
+ require_paths:
50
+ - lib
51
+ required_ruby_version: !ruby/object:Gem::Requirement
52
+ none: false
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ hash: 3
57
+ segments:
58
+ - 0
59
+ version: "0"
60
+ required_rubygems_version: !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ">="
64
+ - !ruby/object:Gem::Version
65
+ hash: 11
66
+ segments:
67
+ - 1
68
+ - 2
69
+ version: "1.2"
70
+ requirements: []
71
+
72
+ rubyforge_project: logit
73
+ rubygems_version: 1.3.7
74
+ signing_key:
75
+ specification_version: 3
76
+ summary: Easily add custom logging abilities.
77
+ test_files: []
78
+