Class: App::Logger

Inherits:
Logger
  • Object
show all
Defined in:
lib/app-logger.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dest = nil, formatter: nil, bunny_formatter: nil) ⇒ Logger

Returns a new instance of Logger.



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/app-logger.rb', line 37

def initialize( dest = nil, formatter: nil, bunny_formatter: nil )
  raise 'Log уже есть.' if defined?( ::Log )
  raise 'Сначала нужны настройки.' unless defined?( ::Cfg )

  @main_formatter = formatter || base_formatter
  @amqp_formatter = bunny_formatter || mq_formatter || @main_formatter

  raise 'formatter должны быть Proc' if ! @main_formatter.is_a?(Proc) || ! @amqp_formatter.is_a?(Proc)

  @logdev = 
    case dest ||= Cfg.app.log
      when 'stderr', 'syslog', nil
        # $stdout.close
        $stdout.reopen $stderr
      when 'stdout'
        # $stderr.close
        $stderr.reopen $stdout
      else
        Cfg.app.log = "#{ Cfg.root }/#{ Cfg.app.log }" unless Cfg.app.log =~ %r{^/}
        FileUtils.mkdir_p Pathname.new( Cfg.app.log ).dirname
        logf = File.open( Cfg.app.log, 'a' )
        logf.sync = true
        $stderr.reopen logf
        $stdout.reopen logf
        logf
    end
  super @logdev, progname: Cfg.app.progname, level: Cfg.loglevel, formatter: @main_formatter
  Kernel.const_set 'Log', self
  Kernel.const_set 'MQLog', ::Logger.new( @logdev, progname: "#{ Cfg.app.progname }+Bunny", level: Cfg.loglevel, formatter: @amqp_formatter )
  Log.info{"#{ Cfg.env } started. thread #{ Thread.current.object_id }."}
  self
end

Instance Attribute Details

#amqp_formatterObject

Returns the value of attribute amqp_formatter.



11
12
13
# File 'lib/app-logger.rb', line 11

def amqp_formatter
  @amqp_formatter
end

#logdevObject

Returns the value of attribute logdev.



11
12
13
# File 'lib/app-logger.rb', line 11

def logdev
  @logdev
end

#main_formatterObject

Returns the value of attribute main_formatter.



11
12
13
# File 'lib/app-logger.rb', line 11

def main_formatter
  @main_formatter
end

Instance Method Details

#base_formatterObject



13
14
15
16
17
18
# File 'lib/app-logger.rb', line 13

def base_formatter
  return proc { |severity, datetime, progname, msg|
#        "#{ severity[0] }°#{ datetime.strftime '%d/%m-%H:%M:%S' }°#{ Thread.current.object_id }°#{ Process.pid }°#{ "%20s" % caller_locations[0..4].last.label }:#{ "%03d" % caller_locations[0..4].last.lineno.to_i }—#{ msg }\n"
      "#{ severity[0] } #{ datetime.strftime '%d/%m-%H:%M:%S' }  #{ msg }\n"
  }
end

#mq_formatterObject



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/app-logger.rb', line 19

def mq_formatter
  # Убираем некоторую вредность из логгера Кролика
  return proc { |severity, datetime, progname, msg|
    if msg !~ /Using TLS but/ && severity != 'DEBUG'
      msg.force_encoding('UTF-8')
      if severity == 'ERROR'
        msg += <<~BACKTACE

          Thread #{ Thread.current.inspect }
          Process #{ Process.pid }
          #{ "===BACKTACE:===\n" + caller.join("\n")  + "\n===" }
        BACKTACE
      end
      base_formatter.call(severity, datetime, progname, '(ѣ)' + msg )
    end
  }
end

#removeObject



70
71
72
73
# File 'lib/app-logger.rb', line 70

def remove
  Kernel.send( :remove_const, 'Log' ) if defined?( Log )
  Kernel.send( :remove_const, 'Log' ) if defined?( MQLog )
end