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# SECUREAUTH LABS. Copyright 2018 SecureAuth Corporation. All rights reserved.
2#
3# This software is provided under under a slightly modified version
4# of the Apache Software License. See the accompanying LICENSE file
5# for more information.
6#
7# Description: This logger is intended to be used by impacket instead
8# of printing directly. This will allow other libraries to use their
9# custom logging implementation.
10#
12import logging
13import sys
15# This module can be used by scripts using the Impacket library
16# in order to configure the root logger to output events
17# generated by the library with a predefined format
19# If the scripts want to generate log entries, they can write
20# directly to the root logger (logging.info, debug, etc).
22class ImpacketFormatter(logging.Formatter):
23 '''
24 Prefixing logged messages through the custom attribute 'bullet'.
25 '''
26 def __init__(self):
27 logging.Formatter.__init__(self,'%(bullet)s %(message)s', None)
29 def format(self, record):
30 if record.levelno == logging.INFO:
31 record.bullet = '[*]'
32 elif record.levelno == logging.DEBUG:
33 record.bullet = '[+]'
34 elif record.levelno == logging.WARNING:
35 record.bullet = '[!]'
36 else:
37 record.bullet = '[-]'
39 return logging.Formatter.format(self, record)
41class ImpacketFormatterTimeStamp(ImpacketFormatter):
42 '''
43 Prefixing logged messages through the custom attribute 'bullet'.
44 '''
45 def __init__(self):
46 logging.Formatter.__init__(self,'[%(asctime)-15s] %(bullet)s %(message)s', None)
48 def formatTime(self, record, datefmt=None):
49 return ImpacketFormatter.formatTime(self, record, datefmt="%Y-%m-%d %H:%M:%S")
51def init(ts=False):
52 # We add a StreamHandler and formatter to the root logger
53 handler = logging.StreamHandler(sys.stdout)
54 if not ts:
55 handler.setFormatter(ImpacketFormatter())
56 else:
57 handler.setFormatter(ImpacketFormatterTimeStamp())
58 logging.getLogger().addHandler(handler)
59 logging.getLogger().setLevel(logging.INFO)