nvflare.fuel.utils.log_utils module

class ANSIColor[source]

Bases: object

COLORS = {'black': '30', 'blue': '34', 'bold_red': '31;1', 'cyan': '36', 'green': '32', 'grey': '38', 'magenta': '35', 'red': '31', 'reset': '0', 'white': '37', 'yellow': '33'}
DEFAULT_LEVEL_COLORS = {'CRITICAL': '31;1', 'DEBUG': '38', 'ERROR': '31', 'INFO': '38', 'NOTSET': '38', 'WARNING': '33'}
classmethod colorize(text: str, color: str) str[source]

Wrap text with the given ANSI SGR color.

Parameters:
  • text (str) – text to colorize.

  • color (str) – ANSI SGR color code or color name defined in ANSIColor.COLORS.

Returns:

colorized text

class BaseFormatter(fmt='%(asctime)s - %(name)s - %(levelname)s - %(message)s', datefmt=None, style='%')[source]

Bases: Formatter

Default formatter for log records.

The following attributes are added to the record and can be configured in fmt with ‘%(<attribute>)s’
  • record.name: base name

  • record.fullName: full name

  • record.fl_ctx: bracked fl ctx key value pairs if exists in the message

  • record.identity: identity from fl_ctx if fl_ctx exists

Parameters:
  • fmt (str) – format string which uses LogRecord attributes.

  • datefmt (str) – date/time format string. Defaults to ‘%Y-%m-%d %H:%M:%S’.

  • style (str) – style character ‘%’ ‘{’ or ‘$’ for format string.

format(record)[source]

Format the specified record as text.

The record’s attribute dictionary is used as the operand to a string formatting operation which yields the returned string. Before formatting the dictionary, a couple of preparatory steps are carried out. The message attribute of the record is computed using LogRecord.getMessage(). If the formatting string uses the time (as determined by a call to usesTime(), formatTime() is called to format the event time. If there is exception information, it is formatted using formatException() and appended to the message.

remove_empty_attributes()[source]
class ColorFormatter(fmt='%(asctime)s - %(name)s - %(levelname)s - %(message)s', datefmt=None, style='%', level_colors={'CRITICAL': '31;1', 'DEBUG': '38', 'ERROR': '31', 'INFO': '38', 'NOTSET': '38', 'WARNING': '33'}, logger_colors={})[source]

Bases: BaseFormatter

Format colors based on log levels. Optionally can provide mapping based on logger names.

Parameters:
  • fmt (str) – format string which uses LogRecord attributes.

  • datefmt (str) – date/time format string. Defaults to ‘%Y-%m-%d %H:%M:%S’.

  • style (str) – style character ‘%’ ‘{’ or ‘$’ for format string.

  • level_colors (Dict[str, str]) – dict of levelname: ANSI color. Defaults to ANSIColor.DEFAULT_LEVEL_COLORS.

  • logger_colors (Dict[str, str]) – dict of loggername: ANSI color. Defaults to {}.

format(record)[source]

Format the specified record as text.

The record’s attribute dictionary is used as the operand to a string formatting operation which yields the returned string. Before formatting the dictionary, a couple of preparatory steps are carried out. The message attribute of the record is computed using LogRecord.getMessage(). If the formatting string uses the time (as determined by a call to usesTime(), formatTime() is called to format the event time. If there is exception information, it is formatted using formatException() and appended to the message.

class JsonFormatter(fmt='%(asctime)s - %(identity)s - %(name)s - %(fullName)s - %(levelname)s - %(fl_ctx)s - %(message)s', datefmt=None, style='%')[source]

Bases: BaseFormatter

Format log records into JSON.

Parameters:
  • fmt (str) – format string which uses LogRecord attributes. Attributes are used for JSON keys.

  • datefmt (str) – date/time format string. Defaults to ‘%Y-%m-%d %H:%M:%S’.

  • style (str) – style character ‘%’ ‘{’ or ‘$’ for format string.

format(record) str[source]

Format the specified record as text.

The record’s attribute dictionary is used as the operand to a string formatting operation which yields the returned string. Before formatting the dictionary, a couple of preparatory steps are carried out. The message attribute of the record is computed using LogRecord.getMessage(). If the formatting string uses the time (as determined by a call to usesTime(), formatTime() is called to format the event time. If there is exception information, it is formatted using formatException() and appended to the message.

formatMessage(record) dict[source]
generate_fmt_dict(fmt: str) dict[source]
class LogMode[source]

Bases: object

CONCISE = 'concise'
FULL = 'full'
RELOAD = 'reload'
VERBOSE = 'verbose'
class LoggerNameFilter(logger_names=['nvflare'], exclude_logger_names=[])[source]

Bases: Filter

Filter log records based on logger names. Additionally allows all log records with levelno > logging.INFO through.

Parameters:
  • logger_names (List[str]) – list of logger names to allow through filter

  • exclude_logger_names (List[str]) – list of logger names to disallow through filter (takes precedence over allowing from logger_names)

filter(record)[source]

Determine if the specified record is to be logged.

Returns True if the record should be logged, or False otherwise. If deemed appropriate, the record may be modified in-place.

matches_name(name, logger_names) bool[source]
add_log_file_handler(log_file_name)[source]
apply_log_config(dict_config, dir_path: str = '', file_prefix: str = '')[source]
configure_logging(workspace: Workspace, job_id: str | None = None, file_prefix: str = '')[source]
dynamic_log_config(config: dict | str, dir_path: str, reload_path: str)[source]
get_module_logger(module=None, name=None)[source]
get_obj_logger(obj)[source]
get_script_logger()[source]
print_logger_hierarchy(package_name='nvflare', level_colors={'CRITICAL': '31;1', 'DEBUG': '38', 'ERROR': '31', 'INFO': '38', 'NOTSET': '38', 'WARNING': '33'})[source]