Log¶
vortex uses the spdlog library for high-performance logging in C++. This module provides a simplified Python wrapper for spdlog so that Python users of vortex can configure logging.
C++
This module exists only as Python bindings. C++ users may configure spdlog directly according to its documentation.
Overview¶
The log module exposes logger objects and sinks. Much like the Python logging framework, loggers create records with a configured name whereas sinks format and process those records. Logger objects have a severity level to determine which events produce records.
Note
vortex configures spdlog for asynchronous record sinking in a single background thread when compiled in release mode. This scheme offloads handling from critical threads and preserves ordering of log records, but temporal ordering with respect to other console output is not guaranteed.
Loggers¶
Create a Logger directly or use a helper function below.
- class vortex.log.Logger¶
Loggers create records and then route them to associated sinks.
- __init__(name, sink=None, sinks=None)¶
Create a logger with the given name and optionally attack the provided sink/sinks. Sinks may be added or removed after the logger is created.
- Parameters:
name (str) – The name of the logger
sink (vortex.log.Sink) – A single sink to attach to this logger. Mutually exclusive with
sinksargument.sinks ([vortex.log.Sink]) – A list of sinks to attach to this logger. Mutually exclusive with
sinkargument.
- property sinks: [vortex.log.Sink]¶
List of attached sinks. Read-only.
Caution
This property is not thread-safe. Do not read this property when adding or removing sinks elsewhere.
- set_pattern(pattern)¶
Change the pattern for this logger. The pattern is applied to all attached sinks as well. See the spdlog reference for pattern fields. The default pattern is
d-%b-%Y %H:%M:%S.%f] %-10n %^(%L) %v%$.- Parameters:
pattern (str) – The new pattern.
- add_sink(sink)¶
Attach a sink to this logger. Attaching a sink that is already attached has no effect.
Caution
This method is not thread-safe. Do not call this method when a log record might be emitted (e.g., when the engine is running) or while manipulating the list of attached sinks elsewhere.
- Parameters:
sink (vortex.log.Sink) – The sink to attach.
- remove_sink(sink)¶
Remove a sink from this logger. Removing a sink that is not attached has no effect.
Caution
This method is not thread-safe. Do not call this method when a log record might be emitted (e.g., when the engine is running) or while manipulating the list of attached sinks elsewhere.
- log(level, msg)¶
Emit a log record with the given severity level. See spdlog’s source for further details.
Level
Value
trace
0
debug
1
info
2
warn
3
error
4
critical
5
off
6
- flush()¶
Flush all attached sinks.
- clone(name)¶
Copy the current logger. This logger and its copy have the same sinks attached.
- Parameters:
name (str) – Name of the copied logger.
- Returns:
vortex.log.Logger – The copy.
Sinks¶
Sinks receive log records and route them for further processing.
- class vortex.log.Sink¶
Abstract base class for all sinks.
- set_pattern(pattern)¶
Set the formatting pattern for this sink. See
Logger.set_pattern.Note
Calling
Logger.set_patternpropagates the new pattern to all attached sinks, overwriting any local changes by this method.
- flush()¶
Ensure all log records have completed processing.
- class vortex.log.StdOutSink¶
Base:
SinkA sink that formats log records to standard out.
All members are inherited.
- class vortex.log.StdErrSink¶
Base:
SinkA sink that formats log records to standard error.
All members are inherited.
- class vortex.log.ConsoleSink¶
An alias for
StdErrSink.
- class vortex.log.PythonSink¶
Base:
SinkA sink that injects log records into the Python logging system. Internally, this sink creates and caches a Python
logging.Loggerof the appropriate name and forwardslogging.LogRecords tologging.Logger.handle(). Severity levels are translated from spdlog to Python after by multiplying their numeric value by multiplying by10. The log scope is the name of theLogger.spdlog
Python
Level
Value
Level
Value
trace
0
NOTSET
0
debug
1
DEBUG
10
info
2
INFO
20
warn
3
WARNING
30
error
4
ERROR
40
critical
5
CRITICAL
50
off
6
Attention
Avoid configuring this sink to accept high-frequency log output (e.g.,
trace-level) as this may degrade logging or application performance.Note
It is most efficient to create a single application-wide
PythonSinksince this avoid duplication of Pythonlogging.Loggers.
Helpers¶
These functions create a logger and sink with a single function call, which is helpful for simple applications or debugging.
Note
These functions are inefficient for larger applications since a new, independent sink is created each call. Larger applications should instead prefer to create sinks manually and attach all required loggers.
Attention
In earlier undocumented versions, these functions returned previously-created loggers if a logger of the same name was requested. This is no longer the case since a new logger and sink pair is created during each call.
- vortex.get_console_logger(name, level=2)¶
Create a
Loggerusing the given name and level and attach a newStdErrSinkto it.- Returns:
vortex.log.Logger – The new logger.
- vortex.get_file_logger(name, filename, level=2)¶
Create a
Loggerusing the given name and level and attach a newFileSinkwith the given filename.- Returns:
vortex.log.Logger – The new logger.
- vortex.get_python_logger(name, scope='vortex', level=2)¶
Create a
Loggerusing the given name and level and attach a newPythonSinkwith the given scope. The logger’s resulting name is{scope}.{name}.- Returns:
vortex.log.Logger – The new logger.