API Reference¶
GLib → Python¶
Python → GLib¶
-
class
glib_log_bridge.python2glib.
GLibLogHandler
(writer, user_data=None, level=0, **kwargs)[source]¶ Python logger handler that directly forwards to an GLib old-style log handler. Example:
>>> obj = GLibLogHandler(GLib.log_default_handler)
Warning
Uses the old-style GLib log API, so only the message, log domain and level are used, other fields are silently dropped. Use
GLibWriterHandler
instead.Note that there is an pre-existing instance at:
Note that since this subclasses
logging.Handler
, view their documentation for more information, such as filters and so on.-
__init__
(writer, user_data=None, level=0, **kwargs)[source]¶ Initializes the instance, basically setting the formatter to
None
and the filter list to empty.
-
-
class
glib_log_bridge.python2glib.
GLibWriterHandler
(writer, user_data=None, level=0, **kwargs)[source]¶ Python logger handler that directly forwards to an GLib logger writer function. Example:
>>> obj = GLibWriterHandler(GLib.log_writer_default)
Note that there are pre-existing instances at:
Note that since this subclasses
logging.Handler
, view their documentation for more information, such as filters and so on.- Parameters
-
__init__
(writer, user_data=None, level=0, **kwargs)[source]¶ Initializes the instance, basically setting the formatter to
None
and the filter list to empty.
-
_convert_fields
(fields)[source]¶ Convert a record fields to an list of
GLib.LogField
.
-
_get_fields
(record, **kwargs)[source]¶ Return fields to use based on the given log record.
See
LoggerHandler._get_fields()
for more information.This implementation will also set
GLIB_DOMAIN
when not set.
-
_get_logfields
(record)[source]¶ Returns the
GLib.LogField
to pass to GLib for the specified record.
-
class
glib_log_bridge.python2glib.
LoggerHandler
(level=0, replace_module_char='-', log_domain_prefix='', log_domain_suffix='')[source]¶ Python logger handle that just forwards message records to the GLib logger.
Note that since this subclasses
logging.Handler
, view their documentation for more information, such as filters and so on.- Parameters
replace_module_char (
str
) – What to replace the dots (logger namespace separator) with when converting. Also seeLoggerHandler.replace_module_char
.log_domain_prefix (
str
) – What it should put before the converted logger name. Also seeLoggerHandler.log_domain_prefix
.log_domain_suffix (
str
) – What it should put after the converted logger name. Also seeLoggerHandler.log_domain_suffix
.
-
__init__
(level=0, replace_module_char='-', log_domain_prefix='', log_domain_suffix='')[source]¶ Initializes the instance, basically setting the formatter to
None
and the filter list to empty.- Parameters
replace_module_char (
str
) – What to replace the dots (logger namespace separator) with when converting. Also seeLoggerHandler.replace_module_char
.log_domain_prefix (
str
) – What it should put before the converted logger name. Also seeLoggerHandler.log_domain_prefix
.log_domain_suffix (
str
) – What it should put after the converted logger name. Also seeLoggerHandler.log_domain_suffix
.
-
_convert_fields_dict
(fields)[source]¶ Modifies a dictionary of the fields to convert their values into GLib Variants, ready to be passed into
GLib.log_variant()
.By default, existing
GLib.Variant
objects are untouched, strings are converted toGlib.Variant
strings, andbytes
objects toGlib.Variant
bytes, as per the official documentation.For other objects
str()
is called and the resulting string is inserted.Note that strings containing an null-byte will be cut off for that point. An warning will be emitted in that case.
-
_get_fields
(record, **kwargs)[source]¶ Return fields to use based on the given log record.
The default implementation will insert the following keys:
MESSAGE
: The formatted messageCODE_FUNC
,CODE_FILE
,CODE_LINE
: Where it loggedPYTHON_MESSAGE
: The unformatted messagePYTHON_MODULE
: What module the log was emitted fromPYTHON_LOGGER
: To what logger name it was supposed to log toPYTHON_TNAME
: Thread NamePYTHON_TID
: Thread ID
The default implementaion will also insert exception information:
PYTHON_EXC
: Exception type with complete namePYTHON_EXC_MESSAGE
: Stringify exception message
Additionally, the default implementation will also insert (and override) values from the
glib_fields
attribute of the record, if it exists and is adict
, whenupdate_from_record
isTrue
(the default).Subclasses can override this function to insert their own values and such. They can use the lower-scoped methods for more control:
-
_get_fields_basic
(record, fields)[source]¶ Insert and return basic essential fields to use based on the given log record.
The default implementation will insert the following keys:
MESSAGE
: The formatted messageCODE_FUNC
,CODE_FILE
,CODE_LINE
: Where it logged
-
_get_fields_exception
(record, fields)[source]¶ Insert and return fields related to the current exception based on the given log record, if they contain them.
The default implementation will insert the following keys:
PYTHON_EXC
: Exception type with complete namePYTHON_EXC_MESSAGE
: Stringify exception message
They won’t be inserted when no exception is available.
-
_get_fields_metadata
(record, fields)[source]¶ Return basic essential fields to use based on the given log record.
The default implementation will insert the following keys:
PYTHON_MESSAGE
: The unformatted messagePYTHON_MODULE
: What module the log was emitted fromPYTHON_LOGGER
: To what logger name it was supposed to log toPYTHON_TNAME
: Thread NamePYTHON_TID
: Thread ID
-
_get_fields_record
(record, fields)[source]¶ Insert and return additional fields specified in the given log record
glib_fields
attribute of the record, if it exists.
-
_get_log_domain
(record)[source]¶ Returns the log domain for the specified record. The default implementation takes
LoggerHandler.log_domain_prefix
andLoggerHandler.log_domain_prefix
into consideration.
-
_level_to_glib
(level, default=gi.repository.GLib.LogLevelFlags.LEVEL_DEBUG)[source]¶ Converts a Python loglevel to a GLib log level. If no mapping exists, use the specified default value.
The default implementation will use the
LoggerHandler._level_to_glib_map
map.- Parameters
level (
int
) –default (
LogLevelFlags
) –
- Return type
LogLevelFlags
-
_level_to_glib_map
: Dict[int, gi.repository.GLib.LogLevelFlags] = {10: gi.repository.GLib.LogLevelFlags.LEVEL_DEBUG, 20: gi.repository.GLib.LogLevelFlags.LEVEL_INFO, 30: gi.repository.GLib.LogLevelFlags.LEVEL_WARNING, 40: gi.repository.GLib.LogLevelFlags.LEVEL_WARNING, 50: gi.repository.GLib.LogLevelFlags.LEVEL_WARNING}¶ Map used to convert from a Python logger level to the GLib Log Level.
-
glib_log_bridge.python2glib.
glibLogHandlerDefault
= <GLibLogHandler (NOTSET)>¶ Python Logger Handler to forward to
GLib.log_default_handler()
.Warning
Uses the old-style GLib log API, so only the message, log domain and level are used, other fields are silently dropped. Use
GLibWriterHandler
or one of their pre-existing instances instead.
-
glib_log_bridge.python2glib.
glibWriterHandlerDefault
= <GLibWriterHandler (NOTSET)>¶ Python Logger Handler to forward to
GLib.log_writer_default()
.
-
glib_log_bridge.python2glib.
glibWriterHandlerJournald
= <GLibWriterHandler (NOTSET)>¶ Python Logger Handler to forward to
GLib.log_writer_journald()
.
-
glib_log_bridge.python2glib.
glibWriterHandlerStandardStreams
= <GLibWriterHandler (NOTSET)>¶ Python Logger Handler to forward to
GLib.log_writer_standard_streams()
.