forked from ly4k/Certipy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger.py
executable file
·73 lines (57 loc) · 2.28 KB
/
logger.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# Impacket - Collection of Python classes for working with network protocols.
#
# SECUREAUTH LABS. Copyright (C) 2019 SecureAuth Corporation. All rights reserved.
#
# This software is provided under a slightly modified version
# of the Apache Software License. See the accompanying LICENSE file
# for more information.
#
# Description:
# This logger is intended to be used by impacket instead
# of printing directly. This will allow other libraries to use their
# custom logging implementation.
#
import logging as _logging
import sys
# This module can be used by scripts using the Impacket library
# in order to configure the root logger to output events
# generated by the library with a predefined format
# If the scripts want to generate log entries, they can write
# directly to the root logger (logging.info, debug, etc).
class ImpacketFormatter(_logging.Formatter):
"""
Prefixing logged messages through the custom attribute 'bullet'.
"""
def __init__(self):
_logging.Formatter.__init__(self, "%(bullet)s %(message)s", None)
def format(self, record):
if record.levelno == _logging.INFO:
record.bullet = "[*]"
elif record.levelno == _logging.DEBUG:
record.bullet = "[+]"
elif record.levelno == _logging.WARNING:
record.bullet = "[!]"
else:
record.bullet = "[-]"
return _logging.Formatter.format(self, record)
class ImpacketFormatterTimeStamp(ImpacketFormatter):
"""
Prefixing logged messages through the custom attribute 'bullet'.
"""
def __init__(self):
_logging.Formatter.__init__(
self, "[%(asctime)-15s] %(bullet)s %(message)s", None
)
def formatTime(self, record, datefmt=None):
return ImpacketFormatter.formatTime(self, record, datefmt="%Y-%m-%d %H:%M:%S")
def init(ts=False):
# We add a StreamHandler and formatter to the root logger
handler = _logging.StreamHandler(sys.stdout)
if not ts:
handler.setFormatter(ImpacketFormatter())
else:
handler.setFormatter(ImpacketFormatterTimeStamp())
_logging.getLogger("certipy").addHandler(handler)
_logging.getLogger("certipy").setLevel(_logging.INFO)
_logging.getLogger("certipy").propagate = False
logging = _logging.getLogger("certipy")