Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_colorize_auto_tty(self):
# with colorize=Colorize.AUTO on a stream connected to a TTY,
# return colorizing formatter
stream = _get_tty_stream()
cls = fancylogger._screenLogFormatterFactory(fancylogger.Colorize.AUTO, stream)
self.assertEqual(cls, coloredlogs.ColoredFormatter)
def init_logger(log_requests=False):
""" Initialize the logger """
logger = logging.getLogger(__name__.split(".")[0])
for handler in logger.handlers: # pragma: no cover
logger.removeHandler(handler)
if os.name == "nt" and "colorama" in sys.modules: # pragma: no cover
colorama.init()
formatter = coloredlogs.ColoredFormatter(fmt="%(asctime)s: %(message)s")
handler = logging.StreamHandler()
handler.setLevel(logging.DEBUG)
handler.setFormatter(formatter)
logger.addHandler(handler)
logger.setLevel(logging.DEBUG)
logger.propagate = False
if log_requests:
requests.packages.urllib3.add_stderr_logger()
level = logging.INFO
# Turn on logging
root = logging.getLogger()
root.setLevel(level)
# shush little ones
for mod in ['scapy.loading', 'scapy.runtime']:
logging.getLogger(mod).setLevel(logging.CRITICAL)
# Colors and formats
ch = logging.StreamHandler(sys.stdout)
ch.setLevel(level)
fh = logging.FileHandler(logfile, 'w')
fh.setLevel(level)
formatter = ColoredFormatter(fmt=FORMAT, field_styles=LOG_FIELD_STYLES)
ch.setFormatter(formatter)
fh.setFormatter(logging.Formatter(FORMAT))
root.addHandler(ch)
root.addHandler(fh)
can use ANSI terminal escape sequences:
* ``Colorize.NEVER`` (default) forces use the plain `logging.Formatter` class;
* ``Colorize.ALWAYS`` forces use of the colorizing formatter;
* ``Colorize.AUTO`` selects the colorizing formatter depending on
whether `stream` is connected to a terminal.
Second argument `stream` is the stream to check in case `colorize`
is ``Colorize.AUTO``.
"""
formatter = logging.Formatter # default
if HAVE_COLOREDLOGS_MODULE:
if colorize == Colorize.AUTO:
# auto-detect
if humanfriendly.terminal.terminal_supports_colors(stream):
formatter = coloredlogs.ColoredFormatter
elif colorize == Colorize.ALWAYS:
formatter = coloredlogs.ColoredFormatter
elif colorize == Colorize.NEVER:
pass
else:
raise ValueError("Argument `colorize` must be one of 'auto', 'always', or 'never'.")
return formatter
# if logger.hasHandlers():
# print(logger.handlers, len(logger.handlers))
# for i, handler in enumerate(logger.handlers):
# print(i, handler)
# logger.removeHandler(handler)
# print(logger.handlers, len(logger.handlers))
# Remove all existing handlers before adding new ones
while logger.hasHandlers():
logger.removeHandler(logger.handlers[0])
# Configure and add the console handler
if LOG_TO_CONSOLE:
console_handler = logging.StreamHandler()
console_handler.setLevel(CONSOLE_LEVEL)
console_format = coloredlogs.ColoredFormatter(
# level_styles=level_styles,
fmt=fmt_console
)
console_handler.setFormatter(console_format)
console_handler.addFilter(TracebackInfoFilter(clear=True))
logger.addHandler(console_handler)
# Configure and add the file handler
if LOG_TO_FILE:
file_handler = logging.FileHandler(file_name)
file_handler.setLevel(FILE_LEVEL)
file_format = logging.Formatter(fmt_file)
file_handler.setFormatter(file_format)
file_handler.addFilter(TracebackInfoFilter(clear=False))
logger.addHandler(file_handler)
def init_logger(model):
""" Initialize the logger. """
logger = logging.getLogger("cumulusci")
# Remove existing handlers
for handler in list(logger.handlers):
handler.stream.flush(force=True)
logger.removeHandler(handler)
# Create the custom handler
formatter = coloredlogs.ColoredFormatter(fmt="%(asctime)s: %(message)s")
handler = LogHandler(model)
handler.setLevel(logging.DEBUG)
handler.setFormatter(formatter)
logger.addHandler(handler)
logger.setLevel(logging.DEBUG)
logger.propagate = False
return logger
from .base import * # noqa
import coloredlogs
ALLOWED_HOSTS = ['*']
TIME_ZONE = 'Asia/Shanghai'
LOGGING = {
'version': 1,
'disable_existing_loggers': True,
'formatters': {
'colored_formatter': {
'()': coloredlogs.ColoredFormatter,
'format': '%(name)s:%(funcName)s:%(lineno)d %(message)s'
}
},
'handlers': {
'console': {
'class': 'logging.StreamHandler',
'formatter': 'colored_formatter'
}
},
'loggers': {
'django': {
'handlers': ['console'],
'level': 'WARNING',
'propagate': False
},
'django.request': {
'color': 'magenta'
},
'levelname': {
'color': 'black',
'bold': True
},
'name': {
'color': 170
},
'programname': {
'color': 'cyan'
}
}
class LogFormatter(coloredlogs.ColoredFormatter):
def __init__(self):
super().__init__(
LOG_FORMAT,
datefmt=DATE_FORMAT
)
def setup_custom_logger(name):
formatter = coloredlogs.ColoredFormatter(
LOG_FORMAT,
datefmt=DATE_FORMAT,
field_styles=FIELD_STYLES
)
handler = logging.StreamHandler()
handler.setFormatter(formatter)