<commit_before>[![Build Status](https://secure.travis-ci.org/wr0ngway/gem_logger.png)](http://travis-ci.org/wr0ngway/gem_logger)
[![Coverage Status](https://coveralls.io/repos/wr0ngway/gem_logger/badge.png?branch=master)](https://coveralls.io/r/wr0ngway/gem_logger?branch=master)

# GemLogger

Allows classes/modules in gems to have logger class/instance methods with a pluggable Logger implementation

## Usage

Include the GemLogger::Logger concern into the classes that need to be able to log.  By default, it will use the standard Ruby logger, if you need a different logger, then configure GemLogger like:

    GemLogger.configure do |config|
    
      # The default logger instance to use
      # (optional, defaults to Logger.new) 
      config.default_logger = CustomLogger.new
    
      # module to include when GemLogger::Logger is included
      # (optional, defaults to GemLogger::BasicLogger) 
      config.logger_concern = SomeModule
    
    end

## Context

It is also possible to add context to your log messages through the context call:

    MyClass.logger.context(:key => 'value').debug("I want context here")
    
as well as include backtraces and context to exceptions by calling the log_exception method with the error and a custom message:

    MyClass.logger.log_exception(StandardError.new('uh oh'), 'This is bad')
    
By default, log_exception uses the ERROR level, but also accepts a level as an option:

    MyClass.logger.log_exception(StandardError.new('uh oh'), 'This is bad', {:level => :debug})
    
Unless otherwise specified, the context is added to the beginning of the log message in a basic key = value format. You can, however, define your own context handler, and pass it in on initialization:

    GemLogger.configure do |config| do
      config.context_handler = MyHandler
    end

The class should implement:
  get_context - initialize and return the context
  add_to_context(key, val) - add a given value to the context at key<commit_msg>README fix.<commit_after>[![Build Status](https://secure.travis-ci.org/wr0ngway/gem_logger.png)](http://travis-ci.org/wr0ngway/gem_logger)
[![Coverage Status](https://coveralls.io/repos/wr0ngway/gem_logger/badge.png?branch=master)](https://coveralls.io/r/wr0ngway/gem_logger?branch=master)

# GemLogger

Allows classes/modules in gems to have logger class/instance methods with a pluggable Logger implementation

## Usage

Include the GemLogger::Logger concern into the classes that need to be able to log.  By default, it will use the standard Ruby logger, if you need a different logger, then configure GemLogger like:

    GemLogger.configure do |config|
    
      # The default logger instance to use
      # (optional, defaults to Logger.new) 
      config.default_logger = CustomLogger.new
    
      # module to include when GemLogger::Logger is included
      # (optional, defaults to GemLogger::BasicLogger) 
      config.logger_concern = SomeModule
    
    end

## Context

It is also possible to add context to your log messages through the context call:

    MyClass.logger.context(:key => 'value').debug("I want context here")
    
as well as include backtraces and context to exceptions by calling the log_exception method with the error and a custom message:

    MyClass.logger.log_exception(StandardError.new('uh oh'), 'This is bad')
    
By default, log_exception uses the ERROR level, but also accepts a level as an option:

    MyClass.logger.log_exception(StandardError.new('uh oh'), 'This is bad', {:level => :debug})
    
Unless otherwise specified, the context is added to the beginning of the log message in a basic key = value format. You can, however, define your own context handler, and pass it in on initialization:

    GemLogger.configure do |config|
      config.context_handler = MyHandler
    end

The class should implement:
  get_context - initialize and return the context
  add_to_context(key, val) - add a given value to the context at key
{'fraction_non_alphanumeric': 0.08192588858079153, 'fraction_numerical': 0.00201663725737333, 'mean_word_length': 3.9974811083123427, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 6, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6997983', 'n_tokens_mistral': 1098, 'n_tokens_neox': 1073, 'n_words': 425}