Skip to content

Commit

Permalink
commit
Browse files Browse the repository at this point in the history
  • Loading branch information
yangdefeng committed Nov 15, 2021
1 parent 987a2aa commit 39f580b
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 19 deletions.
8 changes: 4 additions & 4 deletions nb_log/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
if nb_log_config_default.WARNING_PYCHARM_COLOR_SETINGS:
only_print_on_main_process(
"""
1)使用pycharm时候,建议重新自定义设置pycharm的console里面的主题颜色
设置方式为 打开pycharm的 file -> settings -> Editor -> Color Scheme -> Console Colors 选择monokai,
并重新修改自定义6个颜色,设置Blue为1585FF,Cyan为06B8B8,Green 为 05A53F,Magenta为 ff1cd5,red为FF0207,yellow为FFB009
如果设置为显示背景色快,由于不同版本的pycahrm或主题,可以根据控制台实际显示设置 White 为 1F1F1F, Black 为 FFFFFF,因为背景色是深色,前景色的文字设置为白色比黑色好。
1)使用pycharm时候,强烈建议按下面的重新自定义设置pycharm的console里面的主题颜色,否则颜色显示瞎眼,代码里面规定的颜色只是大概的红黄蓝绿。在不同的ide软件和主题、字体下是不同的显示效果,需要用户自己设置
设置方式为 打开pycharm的 file -> settings -> Editor -> Color Scheme -> Console Colors 选择monokai,点击展开 ANSI colors,
并重新修改自定义6个颜色,设置Blue为 1585FF ,Cyan为 06B8B8 ,Green 为 05A53F ,Magenta为 ff1cd5,red为 FF0207 ,yellow为 FFB009
如果设置为显示背景色快,由于不同版本的pycahrm或主题,可以根据控制台实际显示设置 White 为 1F1F1F, Black 为 FFFFFF ,因为背景色是深色,前景色的文字设置为白色比黑色好。
2)使用xshell或finashell工具连接linux也可以自定义主题颜色,默认使用shell连接工具的颜色也可以。
Expand Down
22 changes: 17 additions & 5 deletions nb_log/log_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import typing
from functools import lru_cache
from logging import FileHandler
from nb_log import nb_log_config_default # noqa
from nb_log import nb_log_config_default # noqa
from nb_log.handlers import *


Expand Down Expand Up @@ -187,6 +187,7 @@ class LogManager(object):
"""
logger_name_list = []
logger_list = []
preset_name__level_map = dict()

def __init__(self, logger_name: typing.Union[str, None] = 'nb_log_default_namespace'):
"""
Expand All @@ -198,6 +199,15 @@ def __init__(self, logger_name: typing.Union[str, None] = 'nb_log_default_namesp
self._logger_name = logger_name
self.logger = logging.getLogger(logger_name)

def preset_log_level(self, log_level_int=20):
"""
提前设置锁定日志级别,当之后再设置该命名空间日志的级别的时候,按照提前预设的级别,无视之后设定的级别。
主要是针对动态初始化的日志,在生成日志之后再去设置日志级别不方便。
:param log_level_int:
:return:
"""
self.preset_name__level_map[self._logger_name] = log_level_int

# 加*是为了强制在调用此方法时候使用关键字传参,如果以位置传参强制报错,因为此方法后面的参数中间可能以后随时会增加更多参数,造成之前的使用位置传参的代码参数意义不匹配。
# noinspection PyAttributeOutsideInit
def get_logger_and_add_handlers(self, log_level_int: int = None, *, is_add_stream_handler=True,
Expand Down Expand Up @@ -275,8 +285,11 @@ def get_logger_and_add_handlers(self, log_level_int: int = None, *, is_add_strea
self._formatter = formatter_template
else:
raise ValueError('设置的 formatter_template 不正确')

self.logger.setLevel(self._logger_level)
if self._logger_name in self.preset_name__level_map:
# print(self.preset_name__level_map)
self.logger.setLevel(self.preset_name__level_map[self._logger_name])
else:
self.logger.setLevel(self._logger_level)
self.__add_handlers()
# self.logger_name_list.append(self._logger_name)
# self.logger_list.append(self.logger)
Expand Down Expand Up @@ -335,8 +348,7 @@ def __add_handlers(self):
self._judge_logger_has_handler_type(ConcurrentDayRotatingFileHandler) or
self._judge_logger_has_handler_type(FileHandler) or
self._judge_logger_has_handler_type(ConcurrentRotatingFileHandler)
) and all(
[self._log_path, self._log_filename]):
) and all([self._log_path, self._log_filename]):
if not os.path.exists(self._log_path):
os.makedirs(self._log_path)
log_file = os.path.join(self._log_path, self._log_filename)
Expand Down
2 changes: 1 addition & 1 deletion nb_log/nb_log_config_default.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ def add_fields(self, log_record, record, message_dict):
"""
LOG_FILE_HANDLER_TYPE 这个值可以设置为 1 2 3 4 5 四种值,
1为使用多进程安全按日志文件大小切割的文件日志,这是本人实现的批量写入日志,减少操作文件锁次数,测试10进程快速写入文件,win上性能比第5种提高了100倍,linux提升5倍
2为多进程安全按天自动切割的文件日志,同一个文件,每天生成一个日志
2为多进程安全按天自动切割的文件日志,同一个文件,每天生成一个新的日志文件。日志文件名字后缀自动加上日期。
3为不自动切割的单个文件的日志(不切割文件就不会出现所谓进程安不安全的问题)
4为 WatchedFileHandler,这个是需要在linux下才能使用,需要借助lograte外力进行日志文件的切割,多进程安全。
5 为第三方的concurrent_log_handler.ConcurrentRotatingFileHandler按日志文件大小切割的文件日志,
Expand Down
1 change: 1 addition & 0 deletions nb_log/set_nb_log_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ def use_config_form_nb_log_config_module():
file_name = sys._getframe(1).f_code.co_filename
try:
m = importlib.import_module('nb_log_config')
importlib.reload(m) # 这行是防止用户在导入框架之前,写了 from nb_log_config import xx 这种,导致 m.__dict__.items() 不包括所有配置变量了。
msg = f'nb_log包 读取到\n "{m.__file__}:1" 文件里面的变量作为优先配置了\n'
# nb_print(msg)
if is_main_process():
Expand Down
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@

setup(
name='nb_log', #
version="5.8",
version="6.3",
description=(
'very sharp color display,monkey patch bulitin print and high-performance multiprocess safe roating file handler,other handlers includeing dintalk ,email,kafka,elastic and so on '
),
Expand Down Expand Up @@ -63,7 +63,7 @@
python setup.py sdist upload -r pypi
python setup.py sdist & twine upload dist/nb_log-5.8.tar.gz
python setup.py sdist ; twine upload dist/nb_log-6.3.tar.gz
twine upload dist/*
Expand Down
10 changes: 5 additions & 5 deletions tests/example.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@

logger = get_logger('lalala',log_filename='jinzhifengzhuang.log',formatter_template=11)

logger.debug(f'debug是绿色,说明是调试的,代码ok')
logger.info('info是天蓝色,日志正常')
logger.warning('黄色yello,有警告了')
logger.error('粉红色说明代码有错误')
logger.critical('血红色,说明发生了严重错误')
logger.debug(f'debug是绿色,说明是调试的,代码ok ')
logger.info('info是天蓝色,日志正常 ')
logger.warning('黄色yello,有警告了 ')
logger.error('粉红色说明代码有错误 ')
logger.critical('血红色,说明发生了严重错误 ')

print('导入nb_log之后的print是强化版的可点击跳转的')

Expand Down
11 changes: 10 additions & 1 deletion tests/test6.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
from nb_log import get_logger
from nb_log import get_logger,LogManager
import requests

get_logger('urllib3')
requests.get("http://www.baidu.com")


LogManager('abcd').preset_log_level(20)
l1 = get_logger('abcd',log_level_int=20)
l2 = get_logger('abcd',log_level_int=30)


l1.debug('能显示不?')

l1.info('能显示不?')
2 changes: 1 addition & 1 deletion tests/test_nb_log_concurrent_file_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def f(x):
# 200万条 45秒
pool = ProcessPoolExecutor(10)
print('start')
for i in range(10):
for i in range(2):
pool.submit(f,i)
pool.shutdown()
print('end')

0 comments on commit 39f580b

Please sign in to comment.