Skip to content

Commit

Permalink
改进 python hikyuu log 多进程日志文件安全
Browse files Browse the repository at this point in the history
  • Loading branch information
fasiondog committed Oct 5, 2024
1 parent 63ab5e6 commit b773a73
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 15 deletions.
18 changes: 9 additions & 9 deletions hikyuu/util/check.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import traceback
import functools
import asyncio
from .mylog import hku_logger
from .mylog import hku_logger, g_hku_logger_lock


class HKUCheckError(Exception):
Expand Down Expand Up @@ -116,10 +116,10 @@ def wrappedFunc(*args, **kargs):
hku_logger.debug(errmsg)
except Exception:
errmsg = "{} [{}.{}]".format(get_exception_info(), func.__module__, func.__name__)
hku_logger.error(errmsg)
if trace:
# traceback.print_exc()
hku_logger.error(traceback.format_exc())
with g_hku_logger_lock:
hku_logger.error(errmsg)
if trace:
hku_logger.error(traceback.format_exc())
if i == (retry - 1):
if callback is not None:
callback(*args, **kargs)
Expand All @@ -129,10 +129,10 @@ def wrappedFunc(*args, **kargs):
raise KeyboardInterrupt()
except:
errmsg = "Unknown error! {} [{}.{}]".format(get_exception_info(), func.__module__, func.__name__)
hku_logger.error(errmsg)
if trace:
# traceback.print_exc()
hku_logger.error(traceback.format_exc())
with g_hku_logger_lock:
hku_logger.error(errmsg)
if trace:
hku_logger.error(traceback.format_exc())
if i == (retry - 1):
if callback is not None:
callback(*args, **kargs)
Expand Down
23 changes: 17 additions & 6 deletions hikyuu/util/mylog.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import traceback
import time
import functools
import multiprocessing


# 统计函数运行时间
Expand Down Expand Up @@ -39,6 +40,8 @@ def wrappedFunc(*args, **kargs):
_logfile.setLevel(logging.WARN)
hku_logger.addHandler(_logfile)

g_hku_logger_lock = multiprocessing.Lock()


def get_default_logger():
return logging.getLogger(hku_logger_name)
Expand Down Expand Up @@ -96,7 +99,8 @@ def hku_warn(msg, *args, **kwargs):
if logger:
logger.warning("{} [{}] ({}:{})".format(msg.format(*args, **kwargs), st.name, st.filename, st.lineno))
else:
hku_logger.warning("{} [{}] ({}:{})".format(msg.format(*args, **kwargs), st.name, st.filename, st.lineno))
with g_hku_logger_lock:
hku_logger.warning("{} [{}] ({}:{})".format(msg.format(*args, **kwargs), st.name, st.filename, st.lineno))


def hku_error(msg, *args, **kwargs):
Expand All @@ -105,7 +109,8 @@ def hku_error(msg, *args, **kwargs):
if logger:
logger.error("{} [{}] ({}:{})".format(msg.format(*args, **kwargs), st.name, st.filename, st.lineno))
else:
hku_logger.error("{} [{}] ({}:{})".format(msg.format(*args, **kwargs), st.name, st.filename, st.lineno))
with g_hku_logger_lock:
hku_logger.error("{} [{}] ({}:{})".format(msg.format(*args, **kwargs), st.name, st.filename, st.lineno))


def hku_fatal(msg, *args, **kwargs):
Expand All @@ -114,7 +119,8 @@ def hku_fatal(msg, *args, **kwargs):
if logger:
logger.critical("{} [{}] ({}:{})".format(msg.format(*args, **kwargs), st.name, st.filename, st.lineno))
else:
hku_logger.critical("{} [{}] ({}:{})".format(msg.format(*args, **kwargs), st.name, st.filename, st.lineno))
with g_hku_logger_lock:
hku_logger.critical("{} [{}] ({}:{})".format(msg.format(*args, **kwargs), st.name, st.filename, st.lineno))


def hku_debug_if(exp, msg, *args, **kwargs):
Expand Down Expand Up @@ -154,7 +160,9 @@ def hku_warn_if(exp, msg, *args, **kwargs):
if logger:
logger.warning("{} [{}] ({}:{})".format(msg.format(*args, **kwargs), st.name, st.filename, st.lineno))
else:
hku_logger.warning("{} [{}] ({}:{})".format(msg.format(*args, **kwargs), st.name, st.filename, st.lineno))
with g_hku_logger_lock:
hku_logger.warning("{} [{}] ({}:{})".format(msg.format(
*args, **kwargs), st.name, st.filename, st.lineno))
if callback:
callback()

Expand All @@ -167,7 +175,8 @@ def hku_error_if(exp, msg, *args, **kwargs):
if logger:
logger.error("{} [{}] ({}:{})".format(msg.format(*args, **kwargs), st.name, st.filename, st.lineno))
else:
hku_logger.error("{} [{}] ({}:{})".format(msg.format(*args, **kwargs), st.name, st.filename, st.lineno))
with g_hku_logger_lock:
hku_logger.error("{} [{}] ({}:{})".format(msg.format(*args, **kwargs), st.name, st.filename, st.lineno))
if callback:
callback()

Expand All @@ -180,7 +189,9 @@ def hku_fatal_if(exp, msg, *args, **kwargs):
if logger:
logger.critical("{} [{}] ({}:{})".format(msg.format(*args, **kwargs), st.name, st.filename, st.lineno))
else:
hku_logger.critical("{} [{}] ({}:{})".format(msg.format(*args, **kwargs), st.name, st.filename, st.lineno))
with g_hku_logger_lock:
hku_logger.critical("{} [{}] ({}:{})".format(
msg.format(*args, **kwargs), st.name, st.filename, st.lineno))
if callback:
callback()

Expand Down

0 comments on commit b773a73

Please sign in to comment.