Logger Module

logger.logger

TcEx Framework Logger module

class tcex.logger.logger.Logger(logger_name: str)[source]

Bases: object

Framework logger module.

_log_app_data()[source]

Log the App data information as a best effort.

_log_environment()[source]

Log the current environment variables.

_log_platform()[source]

Log the current Platform.

_log_python_version()[source]

Log the current Python version.

_log_tc_proxy(inputs: BaseModel)[source]

Log the proxy settings.

Parameters:

inputs – The inputs model.

_log_tcex_version()[source]

Log the current TcEx version number.

static _sanitize_proxy_url(url: Optional[str]) Optional[str][source]

Sanitize a proxy url, masking username and password.

add_api_handler(session_tc: Session, name: Optional[str] = 'api', level: Optional[str] = None)[source]

Add API logging handler.

Parameters:
  • session_tc – An configured instance of request.Session with TC API Auth.

  • name – The name of the handler.

  • level – The level value as a string.

add_cache_handler(name: str)[source]

Add cache logging handler.

Parameters:

name – The name of the handler.

add_pattern_file_handler(name: str, filename: str, level: int, path: str, pattern: str, formatter: Optional[str] = None, handler_key: Optional[str] = None, max_log_count: Optional[int] = 100, thread_key: Optional[str] = None)[source]

Add custom file logging handler.

This handler is intended for service Apps that need to log events based on the current session id. All log event would be in context to a single playbook execution.

Parameters:
  • name – The name of the handler.

  • filename – The name of the logfile.

  • level – The logging level.

  • path – The path for the logfile.

  • formatter – The logging formatter to use.

  • handler_key – Additional properties for handler to thread condition.

  • max_log_count – The maximum number of logs to keep that match the provided pattern.

  • pattern – The pattern used to match the log files.

  • thread_key – Additional properties for handler to thread condition.

add_rotating_file_handler(name: str, filename: str, path: str, backup_count: int, max_bytes: int, level: str, formatter: Optional[str] = None, mode: Optional[str] = 'a')[source]

Add custom file logging handler.

Parameters:
  • name – The name of the handler.

  • filename – The name of the logfile.

  • path – The path for the logfile.

  • backup_count – The maximum # of backup files.

  • max_bytes – The max file size before rotating.

  • level – The logging level.

  • formatter – The logging formatter to use.

  • mode – The write mode for the file.

add_stream_handler(name: Optional[str] = 'sh', formatter: Optional[str] = None, level: Optional[int] = None)[source]

Add stream logging handler.

Parameters:
  • name – The name of the handler.

  • formatter – The logging formatter to use.

  • level – The logging level.

add_thread_file_handler(name: str, filename: str, level: str, path: str, backup_count: Optional[int] = 0, formatter: Optional[str] = None, handler_key: Optional[str] = None, max_bytes: Optional[int] = 0, mode: Optional[str] = 'a', thread_key: Optional[str] = None)[source]

Add custom file logging handler.

This handler is intended for service Apps that need to log events based on the current trigger id. All log events would be in context to a single playbook.

Parameters:
  • name – The name of the handler.

  • filename – The name of the logfile.

  • level – The logging level.

  • path – The path for the logfile.

  • backup_count – The maximum # of backup files.

  • formatter – The logging formatter to use.

  • handler_key – Additional properties for handler to thread condition.

  • max_bytes – The max file size before rotating.

  • mode – The write mode for the file.

  • thread_key – Additional properties for handler to thread condition.

handler_exist(handler_name: str) bool[source]

Remove a file handler by name.

Parameters:

handler_name – The handler name to remove.

Returns:

True if handler current exists

Return type:

bool

property log: Logger

Return logger.

log_info(inputs: BaseModel)[source]

Send System and App data to logs.

Parameters:

inputs – The inputs model.

static log_level(level: str) int[source]

Return proper level from string.

Parameters:

level – The logging level.

Returns:

The logging level as an int.

Return type:

int

remove_handler_by_name(handler_name: str)[source]

Remove a file handler by name.

Parameters:

handler_name – The handler name to remove.

replay_cached_events(handler_name: Optional[str] = 'cache')[source]

Replay cached log events and remove handler.

shutdown()[source]

Close all handlers.

Parameters:

handler_name (str) – The handler name to remove.

update_handler_level(level: str)[source]

Update all handlers log level.

Parameters:

level – The logging level.

logger.api_handler

API Handler Class

class tcex.logger.api_handler.ApiHandler(session, flush_limit=100)[source]

Bases: Handler

Logger handler for ThreatConnect Exchange API logging.

emit(record)[source]

Emit a record.

Parameters:

record (obj) – The record to be logged.

property entries

Return a copy and clear self._entries.

flush()[source]

Close the logger and flush entries.

handle(record)[source]

Override base handle method to add logic that prevents threading deadlocks

log_to_api(entries)[source]

Send log events to the ThreatConnect API

class tcex.logger.api_handler.ApiHandlerFormatter[source]

Bases: Formatter

Logger formatter for ThreatConnect Exchange API logging.

format(record)[source]

Format log record for ThreatConnect API.

Example log event:

[{
    "timestamp": 1478907537000,
    "message": "Test Message",
    "level": "DEBUG"
}]
Parameters:

record (obj) – The record to be logged.

logger.cache_handler

Cache Handler Class

class tcex.logger.cache_handler.CacheHandler(max_cache=100)[source]

Bases: Handler

Logger handler for caching event until all handlers are available.

emit(record)[source]

Emit a record.

Parameters:

record (obj) – The record to be logged.

property events

Return a copy and clear self._events.

logger.rotating_file_handler_custom

API Handler Class

class tcex.logger.rotating_file_handler_custom.RotatingFileHandlerCustom(filename: str, mode: Optional[str] = 'a', maxBytes: Optional[int] = 0, backupCount: Optional[int] = 0, encoding: Optional[str] = None, delay: Optional[bool] = False)[source]

Bases: RotatingFileHandler

Logger handler for ThreatConnect Exchange File logging.

static custom_gzip_namer(name)[source]

Namer for rotating log handler with gz extension.

Parameters:

name – The current name of the logfile.

static custom_gzip_rotator(source: str, dest: str)[source]

Rotate and compress log file.

Parameters:
  • source – The source filename.

  • dest – The destination filename.

logger.thread_file_handler

Thread File Handler Class

class tcex.logger.thread_file_handler.ThreadFileHandler(filename: str, mode: Optional[str] = 'a', maxBytes: Optional[int] = 0, backupCount: Optional[int] = 0, encoding: Optional[str] = None, delay: Optional[bool] = False)[source]

Bases: RotatingFileHandler

Logger handler for ThreatConnect Exchange File logging.

emit(record: object)[source]

Emit a record.

Emit logging events only if handler_key matches thread_key.

Parameters:

record – The record to be logged.

handler_key = None
thread_key = None

logger.trace_logger

Trace Logger Class

class tcex.logger.trace_logger.TraceLogger(name, level=0)[source]

Bases: Logger

Add trace level to logging

findCaller(stack_info=False, stacklevel=1)[source]

Find the caller for the current log event.

Parameters:

stack_info (bool, optional) – Defaults to False.

Returns:

The caller stack information.

Return type:

tuple

trace(msg, *args, **kwargs)[source]

Set trace logging level

Parameters:

msg (str) – The message to be logged.