bmc-daemon-lib 0.3.18 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 2e5609f9590972c691a3756c1af6bd9ad1ee0282
4
- data.tar.gz: 6a3ee2207696ebc991abb0992a8334f5db0e0856
3
+ metadata.gz: 6003c5c0de9402b624030ffc916167fe5d220286
4
+ data.tar.gz: 98df2f514f440a1a8ef06915cfb0aaddbd172a7c
5
5
  SHA512:
6
- metadata.gz: 94186c5a6810b5c363bc6991ed5a8fc4d83cda7db8a3bcdb36442a2793efb411aa47e1dc2c957a90d6d810318556baf9f07dae00e81861c568f9115ce22ac1e9
7
- data.tar.gz: 2c04229dd313885efd3b7cebc8202f8074d4fc46f6e01fe6e86b75445452ac17859f41397fc43a70d36754945c2f7fbad71a1b169a66584f89b787f268b3cb76
6
+ metadata.gz: 4c583268f6fb0e802a5ff8c4ea47ec8fa4ef34534cc14b92be74cda70bcc892cab263f35898d0efef32809a6694a891b18008142ede3d29fee09c470192ab994
7
+ data.tar.gz: 595ad7d343c8880c992e2b4701f079e841205ebe0ae792ecac836a8c5b368dd13501ed0ca508b8821298f9d4f5230654b0d198e0545943c7a035007303a53236
data/.gitignore CHANGED
@@ -9,6 +9,7 @@
9
9
  /test/tmp/
10
10
  /test/version_tmp/
11
11
  /tmp/
12
+ /.idea
12
13
 
13
14
  # Used by dotenv library to load environment variables.
14
15
  # .env
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- bmc-daemon-lib (0.3.18)
4
+ bmc-daemon-lib (0.4.0)
5
5
  chamber (~> 2.9)
6
6
 
7
7
  GEM
@@ -12,7 +12,7 @@ GEM
12
12
  thor (~> 0.19.1)
13
13
  diff-lcs (1.2.5)
14
14
  hashie (3.4.6)
15
- rake (11.2.2)
15
+ rake (11.3.0)
16
16
  rspec (3.5.0)
17
17
  rspec-core (~> 3.5.0)
18
18
  rspec-expectations (~> 3.5.0)
@@ -1,7 +1,7 @@
1
1
  # coding: utf-8
2
2
  Gem::Specification.new do |spec|
3
3
  # Project version
4
- spec.version = "0.3.18"
4
+ spec.version = "0.4.0"
5
5
 
6
6
  # Project description
7
7
  spec.name = "bmc-daemon-lib"
@@ -0,0 +1,97 @@
1
+ require "logger"
2
+ module BmcDaemonLib
3
+ class Logger < Logger
4
+
5
+ DEFAULT_FORMAT = {
6
+ # header: "%s ‡ %d\t%-8s %-12s ",
7
+ header: "%{time} %7{pid} %-6{severity} %-10{pipe} | %{context}",
8
+ # header: "%{time} %7{pid} %-6{severity} %-10{pipe}(-‡-)%{context}",
9
+ time: "%Y-%m-%d %H:%M:%S",
10
+ context: "[%s]",
11
+ text: "%s",
12
+ array: " ·%s",
13
+ hash: " ·%-15s %s",
14
+ trim: 400,
15
+ }
16
+
17
+ def initialize filename, rotation
18
+ # Initialize
19
+ super
20
+ @format = DEFAULT_FORMAT
21
+
22
+ # Import LOGGER_FORMAT if defined
23
+ if (defined?'LOGGER_FORMAT') && (LOGGER_FORMAT.is_a? Hash)
24
+ @format.merge! LOGGER_FORMAT
25
+ end
26
+
27
+ # Define formatter
28
+ self.formatter = proc do |severity, datetime, progname, messages|
29
+ formatter(severity, datetime, progname, messages)
30
+ end
31
+ end
32
+
33
+ def add severity, message, context = nil, details = nil
34
+ # Start from an empty messages list with the main message
35
+ messages = []
36
+ messages << sprintf(@format[:text], message) if message
37
+
38
+ # Add details from array
39
+ details.each do |line|
40
+ messages << sprintf(@format[:array], line)
41
+ end if details.is_a? Array
42
+
43
+ # Add details from hash
44
+ details.each do |key, value|
45
+ messages << sprintf(@format[:hash], key, value)
46
+ end if details.is_a? Hash
47
+
48
+ # Pass all that stuff to my parent
49
+ super severity, messages, context
50
+ end
51
+
52
+ protected
53
+
54
+ def trimmed line
55
+ line.to_s.rstrip[0..@format[:trim].to_i].force_encoding(Encoding::UTF_8)
56
+ end
57
+
58
+ def formatter severity, datetime, context, messages
59
+ # Build header with time and context
60
+ header = @format[:header] % {
61
+ time: datetime.strftime(@format[:time]),
62
+ pid: Process.pid,
63
+ severity: severity,
64
+ pipe: self.progname,
65
+ context: build_context(context)
66
+ }
67
+
68
+ # If we have a plain message, we're done
69
+ return "#{header} #{trimmed(payload)}\n" unless messages.is_a?(Array)
70
+
71
+ # If we have a bunch of lines, prefix them and send them together
72
+ return messages.collect do |line|
73
+ "#{header} #{trimmed(line)}\n"
74
+ end.join
75
+ end
76
+
77
+ private
78
+
79
+ # Builds prefix from @format[:context] and values
80
+ def build_context values
81
+ # Skip if no format defined
82
+ return "[context is not a hash]" unless @format[:context].is_a? Hash
83
+
84
+ # Call the instance's method to get hash values
85
+ return "[log_context is not a hash]" unless values.is_a? Hash
86
+
87
+ # Build each context part
88
+ return @format[:context].collect do |key, format|
89
+ sprintf(format, values[key])
90
+ end.join
91
+
92
+ rescue KeyError, ArgumentError => ex
93
+ return "[context: #{ex.message}]"
94
+ end
95
+
96
+ end
97
+ end
@@ -23,9 +23,5 @@ module BmcDaemonLib
23
23
 
24
24
  protected
25
25
 
26
- def self.trimmed line
27
- line.to_s.rstrip[0..LOG_MESSAGE_TRIM].force_encoding(Encoding::UTF_8)
28
- end
29
-
30
26
  end
31
27
  end
@@ -2,77 +2,54 @@ require "logger"
2
2
 
3
3
  module BmcDaemonLib
4
4
  module LoggerHelper
5
+ # Use accessor to expose logger to Grape, who uses .logger
6
+ attr_accessor :logger
5
7
 
6
8
  protected
7
9
 
8
- def log_info message, details = nil
9
- log Logger::INFO, message, details
10
+ def log_pipe pipe, caller = nil
11
+ @log_pipe = pipe
12
+ @logger = BmcDaemonLib::LoggerPool.instance.get pipe
13
+ @caller = caller
14
+ #Conf.log "log #{@log_pipe}", "log_to pipe: #{pipe} logger class: #{@logger.class}"
15
+ #return @logger
10
16
  end
11
17
 
12
- def log_error message, details = nil
13
- log Logger::ERROR, message, details
18
+ def log_context
19
+ {} # ['DEFAULT', self.class.name.split('::').last]
14
20
  end
15
21
 
16
- def log_debug message, details = nil
17
- log Logger::DEBUG, message, details
22
+ def log_info message, details = nil
23
+ logger.add Logger::INFO, message, get_full_context, details
18
24
  end
19
-
20
- alias info log_info
21
- alias error log_error
22
- alias debug log_debug
23
-
24
- def log severity, message, details = nil
25
- fail "LoggerHelper.log: invalid logger" unless logger.respond_to? :add
26
-
27
- messages = []
28
-
29
- prefix = build_prefix
30
-
31
- # Add main message
32
- messages << sprintf(LOG_MESSAGE_TEXT, prefix, message) if message
33
-
34
- # Add details from array
35
- details.each do |line|
36
- messages << sprintf(LOG_MESSAGE_ARRAY, prefix, line)
37
- end if details.is_a? Array
38
-
39
- # Add details from hash
40
- details.each do |key, value|
41
- messages << sprintf(LOG_MESSAGE_HASH, prefix, key, value)
42
- end if details.is_a? Hash
43
-
44
- # Return all that stuff
45
- logger.add severity, messages
25
+ def log_error message, details = nil
26
+ logger.add Logger::ERROR, message, get_full_context, details
27
+ end
28
+ def log_debug message, details = nil
29
+ logger.add Logger::DEBUG, message, get_full_context, details
46
30
  end
47
31
 
48
32
  private
49
33
 
50
- # Builds prefix if LOG_PREFIX_FORMAT defined and caller has log_prefix method to provide values
51
- def build_prefix
52
- # Skip if no format defined
53
- return unless defined?('LOG_PREFIX_FORMAT')
54
- return unless LOG_PREFIX_FORMAT.is_a? String
55
-
56
- # At start, values is an empty array
57
- values = nil
34
+ def get_full_context
35
+ context = nil
58
36
 
59
- # Call the instance's method
60
- if respond_to?(:log_prefix, true)
61
- values = log_prefix
62
- end
37
+ # Grab the classe's context
38
+ context = log_context() if self.respond_to?(:log_context)
63
39
 
64
- # Change to an array if a simple string
65
- values = [values] if values.is_a? String
40
+ # Initialize an empty context, if log_context returned something else, or it the method was not exposed
41
+ context = {} unless context.is_a? Hash
66
42
 
67
- # Ensure we always have an array (method not found, or log_prefix returning something else)
68
- values = [] unless values.is_a? Array
43
+ # Who is the caller? Guess it from caller's class name if not provided
44
+ context[:caller] ||= self.class.name.to_s.split('::').last
69
45
 
70
- # Finally format the string
71
- return LOG_PREFIX_FORMAT % values.map(&:to_s)
72
-
73
- rescue ArgumentError => ex
74
- return "(LOG_PREFIX_FORMAT has an invalid format)"
46
+ # Return the whole context
47
+ return context
75
48
  end
76
49
 
50
+ # alias info log_info
51
+ # alias error log_error
52
+ # alias debug log_debug
53
+
77
54
  end
78
55
  end
@@ -21,11 +21,11 @@ module BmcDaemonLib
21
21
  filename = Conf.logfile(pipe)
22
22
 
23
23
  # Create the logger and return it
24
- logger = Logger.new(filename, LOG_ROTATION) #, 10, 1024000)
24
+ logger = BmcDaemonLib::Logger.new(filename, LOG_ROTATION) #, 10, 1024000)
25
25
  logger.progname = pipe.to_s.downcase
26
- logger.formatter = LoggerFormatter
27
26
 
28
27
  # Finally return this logger
28
+ # FIXME logger.datetime_format
29
29
  logger
30
30
 
31
31
  rescue Errno::EACCES
@@ -5,9 +5,7 @@ module BmcDaemonLib
5
5
  class MqConsumerException < StandardError; end
6
6
 
7
7
  class MqConsumer < MqEndpoint
8
-
9
8
  include LoggerHelper
10
- attr_reader :logger
11
9
 
12
10
  def subscribe_to_queue name, context = nil
13
11
  log_info "subscribe_to_queue [#{name}]"
@@ -11,14 +11,9 @@ module BmcDaemonLib
11
11
 
12
12
  class MqEndpoint
13
13
  include LoggerHelper
14
- attr_reader :logger
15
14
 
16
15
  protected
17
16
 
18
- def log_prefix
19
- self.class.name.split('::').last
20
- end
21
-
22
17
  def log_message msg_way, msg_topic, msg_key, msg_body = [], msg_attrs = {}
23
18
  # Message header
24
19
  log_info sprintf("%4s %-20s %s", msg_way, msg_topic, msg_key)
@@ -3,12 +3,12 @@ module BmcDaemonLib
3
3
  include LoggerHelper
4
4
 
5
5
  # Class options
6
- attr_reader :logger
7
6
  attr_reader :pool
8
7
  attr_reader :wid
9
8
 
10
9
  def initialize wid, pool = nil
11
10
  # Logger
11
+ # FIXME log_pipe
12
12
  @logger = LoggerPool.instance.get :workers
13
13
  @log_worker_status_changes = true
14
14
 
@@ -16,8 +16,8 @@ module BmcDaemonLib
16
16
  @config = {}
17
17
 
18
18
  # Set thread context
19
- Thread.current.thread_variable_set :pool, (@pool = pool)
20
19
  Thread.current.thread_variable_set :wid, (@wid = wid)
20
+ Thread.current.thread_variable_set :pool, (@pool = pool)
21
21
  Thread.current.thread_variable_set :started_at, Time.now
22
22
  worker_status WORKER_STATUS_STARTING
23
23
 
@@ -28,7 +28,7 @@ module BmcDaemonLib
28
28
  # We're ok, let's start out loop
29
29
  start_loop
30
30
  end
31
- end
31
+ end
32
32
 
33
33
  protected
34
34
 
@@ -42,14 +42,6 @@ module BmcDaemonLib
42
42
  def worker_config
43
43
  end
44
44
 
45
- def log_prefix
46
- [
47
- Thread.current.thread_variable_get(:wid),
48
- Thread.current.thread_variable_get(:jid),
49
- nil
50
- ]
51
- end
52
-
53
45
  def start_loop
54
46
  log_info "start_loop starting", @config
55
47
  loop do
@@ -83,11 +75,6 @@ module BmcDaemonLib
83
75
  # end
84
76
  end
85
77
 
86
- def worker_jid jid
87
- Thread.current.thread_variable_set :jid, jid
88
- Thread.current.thread_variable_set :updated_at, Time.now
89
- end
90
-
91
78
  def config_section key
92
79
  # Debugging
93
80
  @log_worker_status_changes = @debug
@@ -7,7 +7,7 @@ require "time"
7
7
 
8
8
  # Project's libs
9
9
  require_relative "bmc-daemon-lib/conf"
10
- require_relative "bmc-daemon-lib/logger_formatter"
10
+ require_relative "bmc-daemon-lib/logger"
11
11
  require_relative "bmc-daemon-lib/logger_helper"
12
12
  require_relative "bmc-daemon-lib/logger_pool"
13
13
  require_relative "bmc-daemon-lib/worker_base"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bmc-daemon-lib
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.18
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Bruno MEDICI
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-09-19 00:00:00.000000000 Z
11
+ date: 2016-09-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -82,6 +82,7 @@ files:
82
82
  - bmc-daemon-lib.gemspec
83
83
  - lib/bmc-daemon-lib.rb
84
84
  - lib/bmc-daemon-lib/conf.rb
85
+ - lib/bmc-daemon-lib/logger.rb
85
86
  - lib/bmc-daemon-lib/logger_formatter.rb
86
87
  - lib/bmc-daemon-lib/logger_helper.rb
87
88
  - lib/bmc-daemon-lib/logger_pool.rb