mirror of
https://github.com/rbreu/beeref.git
synced 2026-03-11 08:54:28 +00:00
The debug log file still only logs DEBUG and above, so it's less verbose now. TRACE output will only show in the console if the loglevel is set accordingly.
62 lines
1.9 KiB
Python
62 lines
1.9 KiB
Python
# This file is part of BeeRef.
|
|
#
|
|
# BeeRef is free software: you can redistribute it and/or modify
|
|
# it under the terms of the GNU General Public License as published by
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
# (at your option) any later version.
|
|
#
|
|
# BeeRef is distributed in the hope that it will be useful,
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
# GNU General Public License for more details.
|
|
#
|
|
# You should have received a copy of the GNU General Public License
|
|
# along with BeeRef. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
import logging
|
|
import logging.handlers
|
|
import os.path
|
|
|
|
from PyQt6 import QtCore
|
|
|
|
|
|
logging.TRACE = 5
|
|
|
|
|
|
class BeeLogger(logging.Logger):
|
|
|
|
def __init__(self, name, level=logging.NOTSET):
|
|
super().__init__(name, level)
|
|
logging.addLevelName(logging.TRACE, 'TRACE')
|
|
|
|
def trace(self, msg, *args, **kwargs):
|
|
self.log(logging.TRACE, msg, *args, **kwargs)
|
|
|
|
|
|
logging.setLoggerClass(BeeLogger)
|
|
|
|
|
|
class BeeRotatingFileHandler(logging.handlers.RotatingFileHandler):
|
|
"""RotatingFileHandler that creates log directory if necessary."""
|
|
|
|
def __init__(self, filename, **kwargs):
|
|
os.makedirs(os.path.dirname(filename), exist_ok=True)
|
|
super().__init__(filename, **kwargs)
|
|
|
|
|
|
qtlogger = logging.getLogger('Qt')
|
|
|
|
|
|
def qt_message_handler(mode, context, message):
|
|
logfuncs = {
|
|
QtCore.QtMsgType.QtDebugMsg: qtlogger.debug,
|
|
QtCore.QtMsgType.QtInfoMsg: qtlogger.info,
|
|
QtCore.QtMsgType.QtWarningMsg: qtlogger.warning,
|
|
QtCore.QtMsgType.QtCriticalMsg: qtlogger.critical,
|
|
QtCore.QtMsgType.QtFatalMsg: qtlogger.fatal,
|
|
}
|
|
if context and (context.file or context.line or context.function):
|
|
message = (f'{message}: File {context.file}, line {context.line}, '
|
|
f'in {context.function}')
|
|
|
|
logfuncs[mode](message)
|