Class: SemanticLogger::Appender::File
- Defined in:
- lib/semantic_logger/appender/file.rb
Instance Attribute Summary
Attributes inherited from Base
Attributes inherited from Base
Instance Method Summary collapse
-
#flush ⇒ Object
Flush all pending logs to disk.
-
#initialize(filename, level = nil, filter = nil, &block) ⇒ File
constructor
Create a File Logger appender instance.
-
#log(log) ⇒ Object
Pass log calls to the underlying Rails, log4j or Ruby logger trace entries are mapped to debug since :trace is not supported by the Ruby or Rails Loggers.
-
#reopen ⇒ Object
After forking an active process call #reopen to re-open open the file handles etc to resources.
Methods inherited from Base
colorized_formatter, #default_formatter, json_formatter, #level
Methods inherited from Base
#benchmark, default_level, default_level=, #fast_tag, #level, #level=, #payload, #pop_tags, #push_tags, #silence, #tagged, #tags, #with_payload
Constructor Details
#initialize(filename, level = nil, filter = nil, &block) ⇒ File
Create a File Logger appender instance.
Parameters
filename [String|IO]
Name of file to write to.
Or, an IO stream to which to write the log message to.
level [:trace | :debug | :info | :warn | :error | :fatal]
Override the log level for this appender.
Default: SemanticLogger.default_level
filter [Regexp|Proc]
RegExp: Only include log messages where the class name matches the supplied
regular expression. All other messages will be ignored.
Proc: Only include log messages where the supplied Proc returns true
The Proc must return true or false.
Example
require 'semantic_logger'
# Enable trace level logging
SemanticLogger.default_level = :info
# Log to screen
SemanticLogger.add_appender(STDOUT)
# And log to a file at the same time
SemanticLogger::Logger.add_appender('application.log')
logger = SemanticLogger['test']
logger.info 'Hello World'
Example 2. To log all levels to file and only :info and above to screen:
require 'semantic_logger'
# Enable trace level logging
SemanticLogger.default_level = :trace
# Log to screen but only display :info and above
SemanticLogger.add_appender(STDOUT, :info)
# And log to a file at the same time, including all :trace level data
SemanticLogger.add_appender('application.log')
logger = SemanticLogger['test']
logger.info 'Hello World'
56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/semantic_logger/appender/file.rb', line 56 def initialize(filename, level=nil, filter=nil, &block) raise 'filename cannot be null when initializing the SemanticLogging::Appender::File' unless filename @log = if filename.respond_to?(:write) and filename.respond_to?(:close) filename else @filename = filename reopen end # Set the log level and formatter if supplied super(level, filter, &block) end |
Instance Method Details
#flush ⇒ Object
Flush all pending logs to disk.
Waits for all sent documents to be writted to disk
102 103 104 |
# File 'lib/semantic_logger/appender/file.rb', line 102 def flush @log.flush if @log.respond_to?(:flush) end |
#log(log) ⇒ Object
Pass log calls to the underlying Rails, log4j or Ruby logger
trace entries are mapped to debug since :trace is not supported by the
Ruby or Rails Loggers
89 90 91 92 93 94 95 96 97 98 |
# File 'lib/semantic_logger/appender/file.rb', line 89 def log(log) # Ensure minimum log level is met, and check filter return false if (level_index > (log.level_index || 0)) || !(log) # Since only one appender thread will be writing to the file at a time # it is not necessary to protect access to the file with a semaphore # Allow this logger to filter out log levels lower than it's own @log.write(@formatter.call(log, self) << "\n") true end |
#reopen ⇒ Object
After forking an active process call #reopen to re-open open the file handles etc to resources
Note: This method will only work if a String filename was supplied
on the initializer.
75 76 77 78 79 80 81 82 83 84 |
# File 'lib/semantic_logger/appender/file.rb', line 75 def reopen return unless @filename @log = open(@filename, (::File::WRONLY | ::File::APPEND | ::File::CREAT)) # Force all log entries to write immediately without buffering # Allows multiple processes to write to the same log file simultaneously @log.sync = true @log.set_encoding(Encoding::BINARY) if @log.respond_to?(:set_encoding) @log end |