Coverage for /root/GitHubProjects/impacket/impacket/examples/logger.py : 26%

Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1# Impacket - Collection of Python classes for working with network protocols.
2#
3# SECUREAUTH LABS. Copyright (C) 2019 SecureAuth Corporation. All rights reserved.
4#
5# This software is provided under a slightly modified version
6# of the Apache Software License. See the accompanying LICENSE file
7# for more information.
8#
9# Description:
10# This logger is intended to be used by impacket instead
11# of printing directly. This will allow other libraries to use their
12# custom logging implementation.
13#
15import logging
16import sys
18# This module can be used by scripts using the Impacket library
19# in order to configure the root logger to output events
20# generated by the library with a predefined format
22# If the scripts want to generate log entries, they can write
23# directly to the root logger (logging.info, debug, etc).
25class ImpacketFormatter(logging.Formatter):
26 '''
27 Prefixing logged messages through the custom attribute 'bullet'.
28 '''
29 def __init__(self):
30 logging.Formatter.__init__(self,'%(bullet)s %(message)s', None)
32 def format(self, record):
33 if record.levelno == logging.INFO:
34 record.bullet = '[*]'
35 elif record.levelno == logging.DEBUG:
36 record.bullet = '[+]'
37 elif record.levelno == logging.WARNING:
38 record.bullet = '[!]'
39 else:
40 record.bullet = '[-]'
42 return logging.Formatter.format(self, record)
44class ImpacketFormatterTimeStamp(ImpacketFormatter):
45 '''
46 Prefixing logged messages through the custom attribute 'bullet'.
47 '''
48 def __init__(self):
49 logging.Formatter.__init__(self,'[%(asctime)-15s] %(bullet)s %(message)s', None)
51 def formatTime(self, record, datefmt=None):
52 return ImpacketFormatter.formatTime(self, record, datefmt="%Y-%m-%d %H:%M:%S")
54def init(ts=False):
55 # We add a StreamHandler and formatter to the root logger
56 handler = logging.StreamHandler(sys.stdout)
57 if not ts:
58 handler.setFormatter(ImpacketFormatter())
59 else:
60 handler.setFormatter(ImpacketFormatterTimeStamp())
61 logging.getLogger().addHandler(handler)
62 logging.getLogger().setLevel(logging.INFO)