pyer-logger 1.1.0 → 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 +4 -4
  2. data/lib/pyer/logger.rb +53 -49
  3. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 36b25b0fe284bd8915c3b5e6f0eda445d8e593b0280e6ef44a984b6058e43830
4
- data.tar.gz: 88cd6a283b0efdad5b971d50e73562ea2835d12e5007cc2cdfca17d64cf274e6
3
+ metadata.gz: 37b2bd3c55935f4e00ad24afb3eefd3cefba458ab76124aec5d48c8d94864f21
4
+ data.tar.gz: 0dfa99dc327452c4cb2842df8c9bfa2860e331b60d07fcb96e42cbb483aac9be
5
5
  SHA512:
6
- metadata.gz: 5b321bea4ec569f47a6f8efe73404e1c46ae208db70af1634193d8523d711fad80dd5bb23ac7fd3aceeaff249c7b5fda0ce07dbed4aaeba21842b44ed73f6782
7
- data.tar.gz: eb221ae9a9a48a74aebfebdd7cd544418ce2de37d5addfb3db2401fab137f0ad013ef21c670fee050a37ed6c4879540d94fb8a7853f3901c87c5fc9529e65294
6
+ metadata.gz: 59f2837376f51a162cd49801ab6a2a02db831b663c0017b64f5d8ea3d5c7599c3b01caa99f2393213f5791345f40496c6014127f768d357d47314e8b1581c09d
7
+ data.tar.gz: 6005918677f4113ea853ae2fc289b08f4260ad62a11540b64616f6c765769bb8ce835567d836f8bfed84282670b8aedaca6298f9f54d9fcbfc0d5bf01947a547
data/lib/pyer/logger.rb CHANGED
@@ -9,6 +9,7 @@
9
9
  # 2. INFO : generic (useful) information about system operation
10
10
  # 3. WARN : a warning
11
11
  # 4. ERROR : an error condition
12
+ # 5. NONE : no log
12
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
@@ -16,27 +17,25 @@
16
17
  #
17
18
  # == How to create a logger ?
18
19
  #
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)
20
+ # 1. Create a default logger which logs messages to STDOUT
21
+ # log = Logger.new
22
22
  #
23
- # 2. Create a logger for the file which has the specified name.
24
- # log = Logger.new('logfile.log', self.class)
23
+ # 2. Create a logger which logs messages to STDOUT or STDERR.
24
+ # log = Logger.new(STDOUT)
25
+ # log = Logger.new(STDERR)
25
26
  #
26
- # 3. Create a logger which logs messages to a string.
27
- # log = Logger.new(STRING, self.class)
27
+ # 3. Create a logger for the file which has the specified name.
28
+ # log = Logger.new('logfile.log')
28
29
  #
29
- # Notice that self.class argument prints the class name of the caller object.
30
+ # 4. Create a logger which logs messages to a string.
31
+ # log = Logger.new(STRING)
30
32
  #
31
33
  # == How to log a message ?
32
34
  #
33
- # Notice the different methods being used to log messages of various levels.
34
- #
35
35
  # Messages lower than log.level are not sent to output.
36
- #
37
- # Ranking: DEBUG < INFO < WARN < ERROR
38
- #
39
- # Default log.level is DEBUG. That means all messages are emitted.
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.
40
39
  #
41
40
  # 1. Debug message
42
41
  # log.debug "dev info"
@@ -65,6 +64,10 @@
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
@@ -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
@@ -222,3 +225,4 @@ DEBUG = Pyer::Logger::DEBUG
222
225
  INFO = Pyer::Logger::INFO
223
226
  WARN = Pyer::Logger::WARN
224
227
  ERROR = Pyer::Logger::ERROR
228
+ NONE = Pyer::Logger::NONE
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pyer-logger
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Pierre BAZONNARD