Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def setUp(self):
formatter = colorlog.ColoredFormatter(self.logformat)
stream = logging.StreamHandler()
stream.setLevel(logging.DEBUG)
stream.setFormatter(formatter)
self.logger = logging.getLogger('colorlog')
self.logger.setLevel(logging.DEBUG)
self.logger.addHandler(stream)
logger.setLevel(loglevel)
if 'LogFile' in config and config['LogFile'] != '__screen__':
logfile = config['LogFile']
if not os.path.isdir(os.path.dirname(logfile)):
warnings.warn("Logging directory {0} does not exist".format(
os.path.abspath(os.path.dirname(logfile))))
sys.exit(-1)
flog = logging.FileHandler(logfile)
flog.setFormatter(logging.Formatter(
'[%(asctime)s, %(levelno)d, %(module)s] %(message)s', "%H:%M:%S"))
logger.addHandler(flog)
else:
clog = logging.StreamHandler()
formatter = ColoredFormatter(
"%(log_color)s[%(asctime)s %(levelname)-8s%(module)s]%(reset)s "
"%(white)s%(message)s",
datefmt="%H:%M:%S",
reset=True,
log_colors={
'DEBUG': 'cyan',
'INFO': 'green',
'WARNING': 'yellow',
'ERROR': 'red',
'CRITICAL': 'red',
})
clog.setFormatter(formatter)
clog.setLevel(loglevel)
logger.addHandler(clog)
def configure_logging(verbose=False, quiet=False, fail_silently=False, colorize=True):
handler = colorlog.StreamHandler() if colorize else logging.StreamHandler()
formatter = colorlog.ColoredFormatter(
'%(log_color)s%(asctime)s\t%(name)s\t%(levelname)s\t%(message)s'
) if colorize else logging.Formatter(
'%(asctime)s\t%(name)s\t%(levelname)s\t%(message)s'
)
handler.setFormatter(formatter)
root_logger = logging.getLogger("")
root_logger.addHandler(handler)
root_logger.setLevel(
logging.CRITICAL if quiet and fail_silently else
logging.ERROR if quiet else
logging.DEBUG if verbose else
logging.INFO
)
def create_console_handler(verbose_level):
clog = logging.StreamHandler()
formatter = ColoredFormatter(
"%(log_color)s[%(asctime)s %(levelname)-8s%(module)s]%(reset)s "
"%(white)s%(message)s",
datefmt="%H:%M:%S",
reset=True,
log_colors={
'DEBUG': 'cyan',
'INFO': 'green',
'WARNING': 'yellow',
'ERROR': 'red',
'CRITICAL': 'red',
})
clog.setFormatter(formatter)
if verbose_level == 0:
clog.setLevel(logging.WARN)
#!/usr/bin/env python
#coding:utf-8
import os
import colorlog
import logging
from logging import handlers
log_path = 'logs'
if os.path.isdir(log_path) is not True:
os.mkdir(log_path, 0o755)
logfile = os.path.join(log_path, 'wam.log')
handler = colorlog.StreamHandler()
formatter = colorlog.ColoredFormatter(
'%(log_color)s%(asctime)s [%(name)s] [%(levelname)s] %(message)s%(reset)s',
datefmt=None,
reset=True,
log_colors={
'DEBUG': 'cyan',
'INFO': 'green',
'WARNING': 'yellow',
'ERROR': 'red',
'CRITICAL': 'red,bg_white'
},
secondary_log_colors={},
style='%'
)
handler.setFormatter(formatter)
file_handler = handlers.RotatingFileHandler(logfile, maxBytes=(1048576 * 5), backupCount=7)
def setup_logging(verbose_mode):
global VERBOSE
global log
VERBOSE = verbose_mode
log.propagate = False
log.setLevel(logging.DEBUG if verbose_mode else logging.INFO)
logFormatter = ColoredFormatter(
'%(blue)s%(asctime)s%(reset)s [%(log_color)s%(levelname)-5s%(reset)s] %(message)s',
datefmt='%H:%M:%S',
reset=True,
log_colors={
'DEBUG': 'cyan',
'INFO': 'green',
'WARNING': 'yellow',
'ERROR': 'red',
'CRITICAL': 'red,bg_white',
},
secondary_log_colors={},
style='%'
)
# file output
# fileHandler = logging.FileHandler(LOGFILE, mode='w')
def make_formatter():
# Prefix log messages unobtrusively with "log" to distinguish log
# messages more obviously from other text sent to the error stream.
format_string = ("%(bg_black)s%(log_color)slog: %(display_name)s: "
"[%(levelname)s]%(reset)s %(message)s")
colors = colorlog.default_log_colors
colors['DEBUG'] = 'white'
formatter = colorlog.ColoredFormatter(format_string, log_colors=colors)
return formatter
def create_console_handler(verbose_level):
clog = logging.StreamHandler()
formatter = ColoredFormatter(
"%(log_color)s[%(asctime)s %(levelname)-8s%(module)s]%(reset)s "
"%(white)s%(message)s",
datefmt="%H:%M:%S",
reset=True,
log_colors={
'DEBUG': 'cyan',
'INFO': 'green',
'WARNING': 'yellow',
'ERROR': 'red',
'CRITICAL': 'red',
})
clog.setFormatter(formatter)
if verbose_level == 0:
clog.setLevel(logging.WARN)
'formatters': {
'verbose': {
'()': ColoredFormatter,
'format': '%(log_color)s %(levelname)s %(reset)s %(bold_black)s%(name)s%(reset)s %(message)s',
'log_colors': {
'DEBUG': 'fg_white,bg_black',
'INFO': 'fg_black,bg_bold_white',
'WARNING': 'fg_black,bg_bold_yellow',
'ERROR': 'fg_bold_white,bg_bold_red',
'CRITICAL': 'fg_bold_white,bg_bold_red',
},
},
'django.server': {
'()': ColoredFormatter,
'format': '%(log_color)s%(message)s',
'log_colors': {
'INFO': 'bold_black',
'WARNING': 'bold_yellow',
'ERROR': 'bold_red',
'CRITICAL': 'bold_red',
},
},
},
'handlers': {
'console': {
'level': 'DEBUG',
'class': 'logging.StreamHandler',
'formatter': 'verbose',
},
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import logging
from colorlog import ColoredFormatter # type: ignore
SUCCESS = 25
OUTPUT = logging.DEBUG - 1
class NoxFormatter(ColoredFormatter):
def __init__(self, *args, **kwargs):
super(NoxFormatter, self).__init__(*args, **kwargs)
self._simple_fmt = logging.Formatter("%(message)s")
def format(self, record):
if record.levelname == "OUTPUT":
return self._simple_fmt.format(record)
return super(NoxFormatter, self).format(record)
class LoggerWithSuccessAndOutput(logging.getLoggerClass()): # type: ignore
def __init__(self, name, level=logging.NOTSET):
super(LoggerWithSuccessAndOutput, self).__init__(name, level)
logging.addLevelName(SUCCESS, "SUCCESS")
logging.addLevelName(OUTPUT, "OUTPUT")