pyer-logger 1.0.2 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +5 -5
  2. data/lib/pyer/logger.rb +80 -73
  3. metadata +20 -7
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: a7439de8b230e777556ad8338f1f20eea207fcae
4
- data.tar.gz: 26454618ccb4283a7d315cb08fa8d7f072e46bb7
2
+ SHA256:
3
+ metadata.gz: 37b2bd3c55935f4e00ad24afb3eefd3cefba458ab76124aec5d48c8d94864f21
4
+ data.tar.gz: 0dfa99dc327452c4cb2842df8c9bfa2860e331b60d07fcb96e42cbb483aac9be
5
5
  SHA512:
6
- metadata.gz: dd2e3a78cb157f407df769b086f3b473301653766847272385986d6bc7847ec3f37c3a4d653b39b9a362926df0a481bc891f247da37f143055d5d92950bc76be
7
- data.tar.gz: d64485503494cebc39af119faeb6cba7db1dad0ea343218c24825953f8cdec7de40a28c9498d36f9f8816c9f7cdd83011ed03993c471bdd85acd206699ee7f67
6
+ metadata.gz: 59f2837376f51a162cd49801ab6a2a02db831b663c0017b64f5d8ea3d5c7599c3b01caa99f2393213f5791345f40496c6014127f768d357d47314e8b1581c09d
7
+ data.tar.gz: 6005918677f4113ea853ae2fc289b08f4260ad62a11540b64616f6c765769bb8ce835567d836f8bfed84282670b8aedaca6298f9f54d9fcbfc0d5bf01947a547
data/lib/pyer/logger.rb CHANGED
@@ -2,77 +2,80 @@
2
2
  #
3
3
  # This Logger class provides a simple logging utility for Ruby applications.
4
4
  # Log messages are sent to stdout, stderr, a file or a string by a Logger object.
5
- #
5
+ #
6
6
  # The messages will have varying levels reflecting their varying importance.
7
7
  # The levels, and their meanings, are:
8
8
  # 1. DEBUG : low-level information for developers
9
9
  # 2. INFO : generic (useful) information about system operation
10
10
  # 3. WARN : a warning
11
11
  # 4. ERROR : an error condition
12
- #
12
+ # 5. NONE : no log
13
+ #
13
14
  # So each message has a level, and the Logger itself has a level, which acts
14
15
  # as a filter, so you can control the amount of information emitted from the
15
16
  # logger without having to remove actual messages.
16
- #
17
+ #
17
18
  # == How to create a logger ?
18
- #
19
- # 1. Create a logger which logs messages to STDERR/STDOUT.
20
- # log = Logger.new(STDOUT, self.class)
21
- # log = Logger.new(STDERR, self.class)
22
- #
23
- # 2. Create a logger for the file which has the specified name.
24
- # log = Logger.new('logfile.log', self.class)
25
- #
26
- # 3. Create a logger which logs messages to a string.
27
- # log = Logger.new(STRING, self.class)
28
- #
29
- # Notice that self.class argument prints the class name of the caller object.
30
- #
19
+ #
20
+ # 1. Create a default logger which logs messages to STDOUT
21
+ # log = Logger.new
22
+ #
23
+ # 2. Create a logger which logs messages to STDOUT or STDERR.
24
+ # log = Logger.new(STDOUT)
25
+ # log = Logger.new(STDERR)
26
+ #
27
+ # 3. Create a logger for the file which has the specified name.
28
+ # log = Logger.new('logfile.log')
29
+ #
30
+ # 4. Create a logger which logs messages to a string.
31
+ # log = Logger.new(STRING)
32
+ #
31
33
  # == How to log a message ?
32
- #
33
- # Notice the different methods being used to log messages of various levels.
34
34
  #
35
35
  # Messages lower than log.level are not sent to output.
36
+ # Ranking: DEBUG < INFO < WARN < ERROR < NONE
37
+ # Default Logger.level is DEBUG. That means all messages are emitted.
38
+ # Each logger level is initialized with Logger.level and can set a different level.
36
39
  #
37
- # Ranking: DEBUG < INFO < WARN < ERROR
38
- #
39
- # Default log.level is DEBUG. That means all messages are emitted.
40
- #
41
40
  # 1. Debug message
42
41
  # log.debug "dev info"
43
- #
42
+ #
44
43
  # 2. Information
45
44
  # log.info "some informations"
46
- #
45
+ #
47
46
  # 3. Warning message
48
47
  # log.warn "a warning message"
49
- #
48
+ #
50
49
  # 4. Error message
51
50
  # log.error "error is #{ @code }"
52
- #
51
+ #
53
52
  # Messages are provided in a string or in a block, or both.
54
- #
53
+ #
55
54
  # 1. Message in block.
56
55
  # log.error { "Argument 'foo' not given." }
57
- #
56
+ #
58
57
  # 2. Message as a string.
59
58
  # log.error "Argument #{ @foo } mismatch."
60
- #
59
+ #
61
60
  # 3. Both arguments
62
- # log.error("Argument ") { "#{ @foo } mismatch." }
63
- #
61
+ # log.error("Argument ") { "#{ @foo } mismatch." }
62
+ #
64
63
  # == How to set severity level ?
65
64
  #
66
65
  # log.level = INFO
67
- #
66
+ #
67
+ # == How to set default severity level ?
68
+ #
69
+ # Logger.level = NONE
70
+ #
68
71
  # == How to close a logger ?
69
- #
72
+ #
70
73
  # log.close
71
- #
74
+ #
72
75
  # == Installation
73
- #
74
- # gem install pyer-options
75
- #
76
+ #
77
+ # gem install pyer-logger
78
+ #
76
79
  module Pyer
77
80
  require 'stringio'
78
81
 
@@ -84,59 +87,59 @@ module Pyer
84
87
  INFO = 1
85
88
  WARN = 2
86
89
  ERROR = 3
90
+ NONE = 4
87
91
  # Severity label for logging. (max 5 char)
88
- SEVERITY_LABELS = ['DEBUG', 'INFO ', 'WARN ', 'ERROR']
89
- COLOURED_LABELS = ["\033[44mDEBUG\033[m", "\033[42mINFO \033[m", "\033[43mWARN \033[m", "\033[41mERROR\033[m"]
92
+ SEVERITY_LABELS = ['DEBUG', 'INFO ', 'WARN ', 'ERROR', 'NONE']
93
+ COLOURED_LABELS = ["\033[44mDEBUG\033[m", "\033[42mINFO \033[m", "\033[43mWARN \033[m", "\033[41mERROR\033[m", ""]
90
94
  end
91
95
  include Severity
92
96
 
93
97
  STRING = -1
94
98
 
95
- # Logging severity threshold (e.g. <tt>Logger::INFO</tt>).
96
- attr_accessor :level
97
-
98
- # Returns +true+ if the current severity level allows the printing of the message
99
+ @@default_level = DEBUG
99
100
 
100
- def debug?
101
- @level <= DEBUG
101
+ def self.level=(level)
102
+ @@default_level = level
102
103
  end
103
104
 
104
- def info?
105
- @level <= INFO
105
+ def self.level
106
+ @@default_level
106
107
  end
107
108
 
108
- def warn?
109
- @level <= WARN
110
- end
109
+ # Logging severity threshold (e.g. <tt>Logger::INFO</tt>).
110
+ attr_accessor :level
111
111
 
112
- def error?
113
- @level <= ERROR
114
- end
112
+ # Prefix can be the class name
113
+ attr_accessor :prefix
115
114
 
116
115
  # Create an instance.
117
116
  # outputs log messages on STDOUT, STDERR, a file or a StringIO
118
117
  #
119
- def initialize(logdev = nil, klass = nil)
120
- @level = DEBUG
118
+ def initialize(logdev = STDOUT)
119
+ @level = @@default_level
120
+ @prefix = ""
121
121
  @logdev = $stdout
122
122
  @severity_label = COLOURED_LABELS
123
- @klass_name = ''
124
- @klass_name = klass.name unless klass.nil?
125
- return if logdev.nil?
126
- if logdev == STRING
127
- # no log device implies that messages are stored in a string
128
- @logdev = StringIO.new
129
- @severity_label = SEVERITY_LABELS
123
+ if logdev == STDOUT
124
+ @logdev = $stdout
130
125
  else
131
- if logdev == STDOUT
132
- @logdev = $stdout
126
+ if logdev == STDERR
127
+ @logdev = $stderr
133
128
  else
134
- if logdev == STDERR
135
- @logdev = $stderr
136
- else
137
- # the default log device is a file name
138
- @logdev = File.new(logdev.to_s, 'a')
129
+ if logdev == STRING
130
+ # no log device implies that messages are stored in a string
131
+ @logdev = StringIO.new
139
132
  @severity_label = SEVERITY_LABELS
133
+ else
134
+ if logdev.is_a? String
135
+ # the log device is a file name
136
+ @logdev = File.new(logdev.to_s, 'a')
137
+ @severity_label = SEVERITY_LABELS
138
+ else
139
+ # the default is no log
140
+ @level = NONE
141
+ @logdev = $stderr
142
+ end
140
143
  end
141
144
  end
142
145
  end
@@ -179,15 +182,15 @@ module Pyer
179
182
  return if @logdev.nil? || severity < @level
180
183
  message = '' if message.nil?
181
184
  message += block.call if block_given?
182
- @logdev.write(Formatter.new.string(@klass_name, @severity_label[severity], message))
185
+ @logdev.write(Formatter.new.string(@prefix, @severity_label[severity], message))
183
186
  end
184
187
  end
185
188
 
186
189
  # Formatter class
187
190
  class Formatter
188
191
  FORMAT = "%s [%5d] %12s %s %s\n"
189
- def string(klass_name, label, message = nil)
190
- format(FORMAT, format_datetime(Time.now), $$, klass_name, label, format_message(message))
192
+ def string(prefix, label, message = nil)
193
+ format(FORMAT, format_datetime(Time.now), $$, prefix, label, format_message(message))
191
194
  end
192
195
 
193
196
  private
@@ -211,7 +214,10 @@ module Pyer
211
214
  end
212
215
  end
213
216
 
214
- # Backward-compatible alias
217
+ # == Aliases
218
+ #
219
+ # Aliases are made to make life simpler.
220
+ #
215
221
  Logger = Pyer::Logger
216
222
  STRING = Pyer::Logger::STRING
217
223
 
@@ -219,3 +225,4 @@ DEBUG = Pyer::Logger::DEBUG
219
225
  INFO = Pyer::Logger::INFO
220
226
  WARN = Pyer::Logger::WARN
221
227
  ERROR = Pyer::Logger::ERROR
228
+ NONE = Pyer::Logger::NONE
metadata CHANGED
@@ -1,29 +1,43 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pyer-logger
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.2
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Pierre BAZONNARD
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-04-29 00:00:00.000000000 Z
11
+ date:
12
12
  dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: stringio
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '3.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '3.0'
13
27
  - !ruby/object:Gem::Dependency
14
28
  name: rake
15
29
  requirement: !ruby/object:Gem::Requirement
16
30
  requirements:
17
31
  - - "~>"
18
32
  - !ruby/object:Gem::Version
19
- version: '0'
33
+ version: '12.3'
20
34
  type: :development
21
35
  prerelease: false
22
36
  version_requirements: !ruby/object:Gem::Requirement
23
37
  requirements:
24
38
  - - "~>"
25
39
  - !ruby/object:Gem::Version
26
- version: '0'
40
+ version: '12.3'
27
41
  - !ruby/object:Gem::Dependency
28
42
  name: minitest
29
43
  requirement: !ruby/object:Gem::Requirement
@@ -46,7 +60,7 @@ extensions: []
46
60
  extra_rdoc_files: []
47
61
  files:
48
62
  - lib/pyer/logger.rb
49
- homepage: https://github.com/pyer/logger
63
+ homepage: https://github.com/pyer/pyer-logger
50
64
  licenses:
51
65
  - MIT
52
66
  metadata: {}
@@ -65,8 +79,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
65
79
  - !ruby/object:Gem::Version
66
80
  version: '0'
67
81
  requirements: []
68
- rubyforge_project:
69
- rubygems_version: 2.4.5
82
+ rubygems_version: 3.1.2
70
83
  signing_key:
71
84
  specification_version: 4
72
85
  summary: Logger