logaware

Logger

class logaware.logger.AwareLogger[source]

Similar to a logging.Logger but is context aware. There is only one ContextLogger per context and it automatically figures out the module that is being logged from. Other information about the context can also be injected into the log message.

The ContextLogger will use logging.getLogger() to get a Logger based on which module the logger is being called from. For example, if this was being called in logaware.logger, the log message would include logaware.logger as the logger name:

>>> import logging
>>> logging.getLogger().setLevel(logging.DEBUG)
>>> log = AwareLogger()
>>> log.info('Test message').module
'...logaware.logger'
CRITICAL = <LogLevel CRITICAL (50)>

CRITICAL Log level

DEBUG = <LogLevel DEBUG (10)>
ERROR = <LogLevel ERROR (40)>
FATAL = <LogLevel CRITICAL (50)>
INFO = <LogLevel INFO (20)>
WARN = <LogLevel WARNING (30)>
WARNING = <LogLevel WARNING (30)>
critical(*args, **kwargs)

Log CRITICAL level message. See AwareLogger.log() for argument info.

debug(*args, **kwargs)

Log DEBUG level message. See AwareLogger.log() for argument info.

error(*args, **kwargs)

Log ERROR level message. See AwareLogger.log() for argument info.

exception(*args, **kwargs)

Log ERROR level message with traceback. See AwareLogger.log() for argument info.

fatal(*args, **kwargs)

Alias for AwareLogger.critical()

get_level_name(level)[source]

Get the textual representation of logging level.

Parameters:level (int) – Logging level
Returns:Name of logging level
Return type:unicode
info(*args, **kwargs)

Log INFO level message. See AwareLogger.log() for argument info.

isEnabledFor(level)[source]

Is this logger enabled for level ‘level’?

Note: Wrapper around native logger method.

Parameters:level (int) – Logging level
Returns:Whether or not logging is enabled for level.
Return type:boolean
log(level, msg, **kwargs)[source]

Log a message at specified level

Parameters:
  • level (int) – Logging level
  • msg (unicode) – Log message
  • **kwargs – Extra logging parameters and substitution parameters for log message.
Returns:

LogRecord sent to logging handler

Return type:

logging.LogRecord

warn(*args, **kwargs)

Alias for AwareLogger.warning()

warning(*args, **kwargs)

Log WARNING level message. See AwareLogger.log() for argument info.

exception logaware.logger.LogFormatException(message, original)[source]

Exception raised if there is an error processing the substitution format of a message.

class logaware.logger.LogLevel(level, name, traceback=False)[source]

A logging level

Parameters:
  • level (int) – Log level
  • name (unicode) – Level name
  • traceback (bool) – Include traceback when logging
class logaware.logger.LoggerMetaClass[source]

Metaclass that sets up log levels

logaware.logger.log_method_factory(name, level, traceback=False)[source]

Create a method that will log at the specified level

Parameters:
  • name (bytes) – Method name
  • level (LogLevel) – Logging level
  • traceback (bool) – Include traceback when logging

MetaLogger

class logaware.metalogger.LogMeta(**kwargs)[source]

Read only meta data for a log message.

Values must be JSON serializable.

Automatically serializes as JSON when stringified.

to_dict()[source]
Returns:Dictionary of meta data
Return type:dict
class logaware.metalogger.LogMetaManager(meta=None)[source]

Track additional metadata for logging. The metadata is stored on this instance so this instance can not be re-used for multiple requests.

To add other information to the log output, use set_meta:

>>> import logging
>>> logging.getLogger().setLevel(logging.DEBUG)
>>> meta = LogMetaManager()
>>> meta.set_meta(user='foo', nothing=None)
<LogMeta {"user":"foo"}>
>>> log = MetaAwareLogger(getter=lambda: meta.get_meta())
>>> log.info('Test message').meta
<LogMeta {"user":"foo"}>
get_meta()[source]
Returns:Current meta data
Return type:LogMeta
set_meta(**kwargs)[source]

Add metadata to the current meta context

Parameters:**kwargs – Meta data to add to log records. Must be JSON serializable.
Returns:Current meta
Return type:dict
class logaware.metalogger.MetaAwareLogger(getter)[source]

Similar to a AwareLogger and also track additional metadata.

Parameters:getter (callable) – Callable to get the current LogMeta. It’s up to the framework to decide how meta is obtained.