logit 1.0.3 → 1.0.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (5) hide show
  1. data/README.rdoc +16 -5
  2. data/Rakefile +1 -1
  3. data/lib/logit.rb +43 -17
  4. data/logit.gemspec +2 -2
  5. metadata +4 -4
@@ -31,6 +31,15 @@ This will write logs to a publisher.log file in the current directory.
31
31
 
32
32
  logs_to '/var/log/publishing/publisher.log'
33
33
 
34
+ ==== Using a different log method name
35
+
36
+ logs_to :publisher, :log_method => :pub_log
37
+
38
+ def publish
39
+ pub_log.info("doing publish")
40
+ # do stuff...
41
+ end
42
+
34
43
  ==== Adding a progname to log entries
35
44
 
36
45
  logs_to :publisher, :progname => "Publisher #{Process.pid}"
@@ -50,10 +59,6 @@ bytes.
50
59
 
51
60
  The default behavior is to use the default file buffering. Turning this
52
61
  on will cause each message to be written to the log file immediately.
53
- Alternatively, you can control this programmatically like so:
54
-
55
- logger.info("my message")
56
- logger.flush()
57
62
 
58
63
  ==== Also print to stdout
59
64
 
@@ -90,12 +95,18 @@ environment to the log file name. For example:
90
95
 
91
96
  == As a Rails plugin
92
97
 
93
- $ script/plugin install git://github.com/ssayles/logit.git
98
+ $ script/plugin install git://github.com/codemariner/logit.git
94
99
 
95
100
 
96
101
 
97
102
  = Release Notes
98
103
 
104
+ [1.0.4]
105
+ * Removed reference to file handler. Allowing Logger to manage this entirely.
106
+ * Added option :log_method that specifies what the name of the log method should be.
107
+ * Reimplemented flush to grab the underlying LogDevice file.
108
+ * Assigning Logger instance to class level variable.
109
+
99
110
  [1.0.3]
100
111
  * Changed default date format to month-day-year.
101
112
 
data/Rakefile CHANGED
@@ -2,7 +2,7 @@ require 'rubygems'
2
2
  require 'rake'
3
3
  require 'echoe'
4
4
 
5
- Echoe.new('logit', '1.0.3') do |p|
5
+ Echoe.new('logit', '1.0.4') do |p|
6
6
  p.description = 'Easily add custom logging abilities to your Ruby or Rails application.'
7
7
  p.url = 'http://github.com/codemariner/logit'
8
8
  p.author = 'Scott Sayles'
@@ -1,14 +1,14 @@
1
1
  module Logit
2
+
3
+ DEFAULT_OPTS = {:write_mode => 'a', :flush_mode => :default, :log_method => :logger, :stdout => false}
4
+
2
5
  def self.included(base)
3
6
  base.extend ClassMethods
4
7
  end
5
8
 
6
9
  module ClassMethods
7
- DEFAULT_OPTS = {:write_mode => 'a', :flush_mode => :default, :stdout => false}
8
10
 
9
- begin
10
- Module.const_get(:Logger)
11
- rescue NameError
11
+ unless Object.const_defined?(:Logger)
12
12
  require 'logger'
13
13
  end
14
14
 
@@ -19,6 +19,7 @@ module Logit
19
19
  # * +:progname+ - Logging program name. The <tt>:progname</tt> value is used in the default logging format if defined.
20
20
  # * +:flush_mode+ - One of <tt>:immediate</tt> or <tt>:default</tt>. <tt>:immediate</tt> will cause a write to the log file for each message logged. The default behavior is to use default file buffering.
21
21
  # * +:stdout+ - If set to <tt>true</tt>, this will cause logs to be printed to stdout <b>in addition to</b> the log file.
22
+ # * +:log_method+ - The name of the instance method to define to access the logger instance. Defaults to <tt>:logger</tt>.
22
23
  #
23
24
  # === Examples
24
25
  #
@@ -42,11 +43,22 @@ module Logit
42
43
  # logger.info("doing something")
43
44
  # end
44
45
  # end
46
+ #
47
+ # class Publisher3
48
+ # include Logit
49
+ #
50
+ # logs_to :publisher, :log_method => :pub_log
51
+ #
52
+ # def do_it
53
+ # pub_log.info("doing something")
54
+ # end
55
+ # end
45
56
  #
46
57
  def logs_to(name, opts={})
47
58
  opts = DEFAULT_OPTS.merge(opts)
48
59
  path = logit_log_name(name, opts)
49
- self.send :define_method, :logger do
60
+
61
+ self.class.send :define_method, :logit_logger do
50
62
  unless @logger
51
63
  @logger = Logit::Logger.new(path, opts)
52
64
  if opts[:progname]
@@ -55,6 +67,10 @@ module Logit
55
67
  end
56
68
  @logger
57
69
  end
70
+
71
+ self.send :define_method, opts[:log_method] do
72
+ self.class.send :logit_logger
73
+ end
58
74
  end
59
75
 
60
76
 
@@ -112,20 +128,18 @@ module Logit
112
128
  #
113
129
  # when running under Rails.
114
130
  #
115
- # You can flush the log by calling logger.flush().
116
- #
117
131
  class Logger < Logger
118
132
 
119
- DEFAULT_OPTS = {:write_mode => 'a', :flush_mode => :default}
120
-
121
133
  def initialize(log_path, opts = DEFAULT_OPTS)
122
134
  @opts = DEFAULT_OPTS.merge(opts)
123
- @f = File.open(log_path, @opts[:write_mode])
124
- super @f
135
+ f = File.open(log_path, @opts[:write_mode])
136
+ if (opts[:flush_mode] == :immediate)
137
+ f.sync = true
138
+ end
139
+ super f
125
140
  end
126
141
 
127
142
  def format_message(severity, timestamp, progname, msg)
128
-
129
143
  name = (progname) ? " [#{progname}]" : ""
130
144
 
131
145
  message = "#{timestamp.strftime('%m-%d-%Y %H:%M:%S')} #{severity.ljust(6)}#{name}: #{msg}\n"
@@ -135,14 +149,26 @@ module Logit
135
149
 
136
150
  def add(severity, message = nil, progname = nil, &block)
137
151
  super(severity, message, progname, &block)
138
- flush() if @opts[:flush_mode] == :immediate
139
152
  end
140
153
 
141
- # Causes any pending writes to be flushed to disk
142
- def flush()
143
- @f.flush()
154
+ # flushes any buffered content to the log
155
+ def flush
156
+ # get a handle to the actual file and then synchronize on the mutex
157
+ # that LogDevice is using (so we don't have the file closed or
158
+ # swapped out from under us)
159
+ if @logdev
160
+ mutex = @logdev.instance_variable_get('@mutex')
161
+ if (mutex)
162
+ mutex.synchronize do
163
+ dev = @logdev.dev
164
+ if (dev and !dev.closed?)
165
+ dev.flush()
166
+ end
167
+ end
168
+ end
169
+ end
170
+ true
144
171
  end
145
-
146
172
  end
147
173
 
148
174
  end
@@ -2,11 +2,11 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = %q{logit}
5
- s.version = "1.0.3"
5
+ s.version = "1.0.4"
6
6
 
7
7
  s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
8
8
  s.authors = ["Scott Sayles"]
9
- s.date = %q{2010-12-10}
9
+ s.date = %q{2011-02-01}
10
10
  s.description = %q{Easily add custom logging abilities to your Ruby or Rails application.}
11
11
  s.email = %q{ssayles@users.sourceforge.net}
12
12
  s.extra_rdoc_files = ["LICENSE", "README.rdoc", "lib/logit.rb"]
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: logit
3
3
  version: !ruby/object:Gem::Version
4
- hash: 17
4
+ hash: 31
5
5
  prerelease: false
6
6
  segments:
7
7
  - 1
8
8
  - 0
9
- - 3
10
- version: 1.0.3
9
+ - 4
10
+ version: 1.0.4
11
11
  platform: ruby
12
12
  authors:
13
13
  - Scott Sayles
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-12-10 00:00:00 -05:00
18
+ date: 2011-02-01 00:00:00 -05:00
19
19
  default_executable:
20
20
  dependencies: []
21
21