bittensor.btlogging.loggingmachine

Contents

bittensor.btlogging.loggingmachine#

Module provides a logging framework for Bittensor, managing both Bittensor-specific and third-party logging states. It leverages the StateMachine from the statemachine package to transition between different logging states such as Default, Debug, Trace, and Disabled.

Classes#

LoggingConfig

Named tuple to hold the logging configuration.

LoggingMachine

Handles logger states for bittensor and 3rd party libraries.

Module Contents#

class bittensor.btlogging.loggingmachine.LoggingConfig[source]#

Bases: NamedTuple

Named tuple to hold the logging configuration.

debug: bool#
trace: bool#
record_log: bool#
logging_dir: str#
class bittensor.btlogging.loggingmachine.LoggingMachine(config, name=BITTENSOR_LOGGER_NAME)[source]#

Bases: statemachine.StateMachine, logging.Logger

Handles logger states for bittensor and 3rd party libraries.

Initialize the logger with a name and an optional level.

Parameters:
Default#
Debug#
Trace#
Disabled#
enable_default#
enable_trace#
enable_debug#
disable_trace#
disable_debug#
disable_logging#
_queue#
_primary_loggers#
_config#
_stream_formatter#
_file_formatter#
_handlers#
_listener#
_logger#
_enable_initial_state(config)[source]#

Set correct state action on initializing

_extract_logging_config(config)[source]#

Extract btlogging’s config from bittensor config

Return type:

dict

_configure_handlers(config)[source]#
Return type:

list[logging.Handler]

get_config()[source]#
set_config(config)[source]#

Set config after initialization, if desired.

_create_and_start_listener(handlers)[source]#

A listener to receive and publish log records.

This listener receives records from a queue populated by the main bittensor logger, as well as 3rd party loggers

get_queue()[source]#

Get the queue the QueueListener is publishing from.

To set up logging in a separate process, a QueueHandler must be added to all the desired loggers.

_initialize_bt_logger(name)[source]#

Initialize logging for bittensor.

Since the initial state is Default, logging level for the module logger is INFO, and all third-party loggers are silenced. Subsequent state transitions will handle all logger outputs.

_deinitialize_bt_logger(name)[source]#

Find the logger by name and remove the queue handler associated with it.

_create_file_handler(logfile)[source]#
Parameters:

logfile (str)

register_primary_logger(name)[source]#

Register a logger as primary logger

This adds a logger to the _primary_loggers set to ensure it doesn’t get disabled when disabling third-party loggers. A queue handler is also associated with it.

Parameters:

name (str)

deregister_primary_logger(name)[source]#

De-registers a primary logger

This function removes the logger from the _primary_loggers set and deinitializes its queue handler

Parameters:

name (str)

enable_third_party_loggers()[source]#

Enables logging for third-party loggers by adding a queue handler to each.

disable_third_party_loggers()[source]#

Disables logging for third-party loggers by removing all their handlers.

_enable_file_logging(logfile)[source]#
Parameters:

logfile (str)

before_transition(event, state)[source]#

Stops listener after transition.

after_transition(event, state)[source]#

Starts listener after transition.

before_enable_default()[source]#

Logs status before enable Default.

after_enable_default()[source]#
before_enable_trace()[source]#

Logs status before enable Trace.

after_enable_trace()[source]#

Logs status after enable Trace.

before_disable_trace()[source]#

Logs status before disable Trace.

after_disable_trace()[source]#

Logs status after disable Trace.

before_enable_debug()[source]#

Logs status before enable Debug.

after_enable_debug()[source]#

Logs status after enable Debug.

before_disable_debug()[source]#

Logs status before disable Debug.

after_disable_debug()[source]#

Logs status after disable Debug.

before_disable_logging()[source]#

Prepares the logging system for disabling.

This method performs the following actions: 1. Logs an informational message indicating that logging is being disabled. 2. Disables trace mode in the stream formatter. 3. Sets the logging level to CRITICAL for all loggers.

This ensures that only critical messages will be logged after this method is called.

property __trace_on__: bool#

Checks if the current state is in “Trace” mode.

Returns:

True if the current state is “Trace”, otherwise False.

Return type:

bool

trace(msg='', prefix='', suffix='', *args, **kwargs)[source]#

Wraps trace message with prefix and suffix.

debug(msg='', prefix='', suffix='', *args, **kwargs)[source]#

Wraps debug message with prefix and suffix.

info(msg='', prefix='', suffix='', *args, **kwargs)[source]#

Wraps info message with prefix and suffix.

success(msg='', prefix='', suffix='', *args, **kwargs)[source]#

Wraps success message with prefix and suffix.

warning(msg='', prefix='', suffix='', *args, **kwargs)[source]#

Wraps warning message with prefix and suffix.

error(msg='', prefix='', suffix='', *args, **kwargs)[source]#

Wraps error message with prefix and suffix.

critical(msg='', prefix='', suffix='', *args, **kwargs)[source]#

Wraps critical message with prefix and suffix.

exception(msg='', prefix='', suffix='', *args, **kwargs)[source]#

Wraps exception message with prefix and suffix.

on()[source]#

Enable default state.

off()[source]#

Disables all states.

set_debug(on=True)[source]#

Sets Debug state.

Parameters:

on (bool)

set_trace(on=True)[source]#

Sets Trace state.

Parameters:

on (bool)

get_level()[source]#

Returns Logging level.

Return type:

int

check_config(config)[source]#
Parameters:

config (bittensor.config)

help()[source]#
classmethod add_args(parser, prefix=None)[source]#

Accept specific arguments fro parser

Parameters:
classmethod config()[source]#

Get config from the argument parser.

Returns:

config object

Return type:

config (bittensor.config)

__call__(config=None, debug=None, trace=None, record_log=None, logging_dir=None)[source]#
Parameters: