Class: GreenSyslogger
- Inherits:
-
Object
- Object
- GreenSyslogger
- Defined in:
- lib/green_syslogger.rb
Overview
Custom Rails Logger
-
Use Syslog like back storage
-
Posibility to use custom ‘facility’
-
Posibility to use custom ‘tag’
-
Posibility to use custom ‘tag’ only in a concrete point so you can configure Syslog to filter this concrete message to another file
-
Compatible with ‘config.logger.auto_flushing = false` so every log of a simple request will be written on an atomic way
require 'green_syslogger' logger = GreenSyslogger.new logger.debug( 'debug message' )
To configure it on the <RAILS_ENVIRONMENT>.rb:
config.logger = GreenSyslogger.new([<default tag>], [<facility>], [<level>])
By default
GreenSyslogger.new('rails', 'local2', :debug)
Example for Rails configuration:
config.log_level = :info
config.logger = GreenSyslogger.new('myapp', 'local1', config.log_level)
config.colorize_logging = false
config.logger.auto_flushing = false
To use it:
logger.debug("my debug message")
logger.error("my error message")
To use another tag:
logger.custom("my custom message", [<tag>], [<level>])
By default:
logger.custom("my custom message", 'custom', :info)
Constant Summary collapse
- LEVELS_MAP =
Mapping between Logger levels and Syslog levels
{ :debug => [:debug , 0], :info => [:info , 1], :warn => [:warning , 2], :error => [:err , 3], :fatal => [:emerg , 4], :unknown => [:debug , 5] }
- DEFAULT_MAX_BUFFER_SIZE =
500
- DATE_FORMAT =
'%Y-%m-%d %H:%M:%S'
- SEPARATOR =
'---'
- RAILS_REQUEST_INIT =
"\n\nStarted"
- RAILS_REQUEST_INIT_CLEANED =
"Started"
- RAILS_REQUEST_INIT_REGEX =
/\n\nStarted/
- @@syslog =
nil
- @@guard =
nil
Instance Attribute Summary collapse
-
#level ⇒ Object
Returns the value of attribute level.
Instance Method Summary collapse
-
#auto_flushing=(value) ⇒ Object
If it is set to ‘true’ every line will be sent in the time is wrotten If it is set to ‘false’ the buffer will be flushed at the end of the request by Rails There is a DEFAULT_MAX_BUFFER_SIZE security buffer size.
- #close ⇒ Object
-
#custom(message, tag = 'custom', level = :info) ⇒ Object
This method is a kind of a back door, using it you can change the ‘tag’ with this message is tagged.
- #flush ⇒ Object
-
#initialize(tag = 'rails', facility = 'local2', level = :debug) ⇒ GreenSyslogger
constructor
A new instance of GreenSyslogger.
Constructor Details
#initialize(tag = 'rails', facility = 'local2', level = :debug) ⇒ GreenSyslogger
Returns a new instance of GreenSyslogger.
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/green_syslogger.rb', line 67 def initialize(tag = 'rails', facility = 'local2', level = :debug) @tag = tag @facility = facility @level = level @max_buffer_size = 1 @buffer = {} if( !@@guard ) @@guard = Mutex.new end if( !@@syslog || !@@syslog.opened? ) @@syslog = Syslog.open(@tag, Syslog::LOG_PID, Syslog.const_get("LOG_#{@facility.upcase}")) end end |
Instance Attribute Details
#level ⇒ Object
Returns the value of attribute level.
45 46 47 |
# File 'lib/green_syslogger.rb', line 45 def level @level end |
Instance Method Details
#auto_flushing=(value) ⇒ Object
If it is set to ‘true’ every line will be sent in the time is wrotten If it is set to ‘false’ the buffer will be flushed at the end of the request by Rails There is a DEFAULT_MAX_BUFFER_SIZE security buffer size
only true or false
113 114 115 |
# File 'lib/green_syslogger.rb', line 113 def auto_flushing=(value) @max_buffer_size = value ? 1 : DEFAULT_MAX_BUFFER_SIZE end |
#close ⇒ Object
83 84 85 86 |
# File 'lib/green_syslogger.rb', line 83 def close flush @@syslog.close end |
#custom(message, tag = 'custom', level = :info) ⇒ Object
This method is a kind of a back door, using it you can change the ‘tag’ with this message is tagged.
level default to ‘info’ tag default ‘ids-custom’
93 94 95 |
# File 'lib/green_syslogger.rb', line 93 def custom(, tag = 'custom', level = :info) add(tag, level, ) if(level?(level)) end |
#flush ⇒ Object
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/green_syslogger.rb', line 117 def flush @@guard.synchronize do unless buffer.empty? copy_buffer = buffer copy_buffer.each do |date, tag, level, | = "[#{date.strftime(DATE_FORMAT)}] #{}" if(tag == @tag) @@syslog.send(LEVELS_MAP[level][0], ) else @@syslog = Syslog.reopen(tag, Syslog::LOG_PID, Syslog.const_get("LOG_#{@facility.upcase}")) @@syslog.send(LEVELS_MAP[level][0], ) @@syslog = Syslog.reopen(@tag, Syslog::LOG_PID, Syslog.const_get("LOG_#{@facility.upcase}")) end end end @buffer.delete(Thread.current) # clear buffer end end |