2019-07-13 18:45:48 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
# This file is part of the Calibre-Web (https://github.com/janeczku/calibre-web)
|
|
|
|
# Copyright (C) 2019 pwr
|
|
|
|
#
|
|
|
|
# This program 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.
|
|
|
|
#
|
|
|
|
# This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
import os
|
2019-11-16 09:09:34 +00:00
|
|
|
import sys
|
2019-07-13 18:45:48 +00:00
|
|
|
import inspect
|
|
|
|
import logging
|
|
|
|
from logging import Formatter, StreamHandler
|
|
|
|
from logging.handlers import RotatingFileHandler
|
|
|
|
|
2019-07-14 06:18:45 +00:00
|
|
|
from .constants import CONFIG_DIR as _CONFIG_DIR
|
2019-07-13 18:45:48 +00:00
|
|
|
|
|
|
|
|
|
|
|
ACCESS_FORMATTER_GEVENT = Formatter("%(message)s")
|
|
|
|
ACCESS_FORMATTER_TORNADO = Formatter("[%(asctime)s] %(message)s")
|
|
|
|
|
|
|
|
FORMATTER = Formatter("[%(asctime)s] %(levelname)5s {%(name)s:%(lineno)d} %(message)s")
|
|
|
|
DEFAULT_LOG_LEVEL = logging.INFO
|
2019-07-14 06:18:45 +00:00
|
|
|
DEFAULT_LOG_FILE = os.path.join(_CONFIG_DIR, "calibre-web.log")
|
|
|
|
DEFAULT_ACCESS_LOG = os.path.join(_CONFIG_DIR, "access.log")
|
2019-07-13 18:45:48 +00:00
|
|
|
LOG_TO_STDERR = '/dev/stderr'
|
2019-11-16 09:09:34 +00:00
|
|
|
LOG_TO_STDOUT = '/dev/stdout'
|
2019-07-13 18:45:48 +00:00
|
|
|
|
|
|
|
logging.addLevelName(logging.WARNING, "WARN")
|
|
|
|
logging.addLevelName(logging.CRITICAL, "CRIT")
|
|
|
|
|
|
|
|
|
2020-12-12 07:11:00 +00:00
|
|
|
class _Logger(logging.Logger):
|
|
|
|
|
2022-03-12 16:14:54 +00:00
|
|
|
def error_or_exception(self, message, stacklevel=2, *args, **kwargs):
|
2024-03-19 00:05:04 +00:00
|
|
|
is_debug = self.getEffectiveLevel() <= logging.DEBUG
|
2021-01-12 19:51:49 +00:00
|
|
|
if sys.version_info > (3, 7):
|
2024-03-19 00:05:04 +00:00
|
|
|
if is_debug:
|
2021-12-25 09:35:08 +00:00
|
|
|
self.exception(message, stacklevel=stacklevel, *args, **kwargs)
|
2021-01-12 19:51:49 +00:00
|
|
|
else:
|
2021-12-25 09:35:08 +00:00
|
|
|
self.error(message, stacklevel=stacklevel, *args, **kwargs)
|
2020-12-12 07:11:00 +00:00
|
|
|
else:
|
2024-03-19 00:05:04 +00:00
|
|
|
if is_debug:
|
2021-12-25 09:35:08 +00:00
|
|
|
self.exception(message, stack_info=True, *args, **kwargs)
|
2021-01-12 19:51:49 +00:00
|
|
|
else:
|
|
|
|
self.error(message, *args, **kwargs)
|
|
|
|
|
2021-01-03 18:27:24 +00:00
|
|
|
def debug_no_auth(self, message, *args, **kwargs):
|
2021-04-22 17:14:56 +00:00
|
|
|
message = message.strip("\r\n")
|
2021-01-03 18:27:24 +00:00
|
|
|
if message.startswith("send: AUTH"):
|
2021-04-22 17:14:56 +00:00
|
|
|
self.debug(message[:16], *args, **kwargs)
|
2021-01-03 18:27:24 +00:00
|
|
|
else:
|
2021-04-22 17:14:56 +00:00
|
|
|
self.debug(message, *args, **kwargs)
|
2021-01-03 18:27:24 +00:00
|
|
|
|
2020-12-12 07:11:00 +00:00
|
|
|
|
2019-07-13 18:45:48 +00:00
|
|
|
def get(name=None):
|
|
|
|
return logging.getLogger(name)
|
|
|
|
|
2022-05-21 20:52:59 +00:00
|
|
|
|
2019-07-13 18:45:48 +00:00
|
|
|
def create():
|
|
|
|
parent_frame = inspect.stack(0)[1]
|
|
|
|
if hasattr(parent_frame, 'frame'):
|
|
|
|
parent_frame = parent_frame.frame
|
|
|
|
else:
|
|
|
|
parent_frame = parent_frame[0]
|
|
|
|
parent_module = inspect.getmodule(parent_frame)
|
|
|
|
return get(parent_module.__name__)
|
|
|
|
|
2022-05-21 20:52:59 +00:00
|
|
|
|
2019-07-13 18:45:48 +00:00
|
|
|
def is_debug_enabled():
|
|
|
|
return logging.root.level <= logging.DEBUG
|
|
|
|
|
2022-05-21 20:52:59 +00:00
|
|
|
|
2019-07-13 18:45:48 +00:00
|
|
|
def is_info_enabled(logger):
|
|
|
|
return logging.getLogger(logger).level <= logging.INFO
|
|
|
|
|
|
|
|
|
|
|
|
def get_level_name(level):
|
|
|
|
return logging.getLevelName(level)
|
|
|
|
|
|
|
|
|
|
|
|
def is_valid_logfile(file_path):
|
2020-03-28 06:13:51 +00:00
|
|
|
if file_path == LOG_TO_STDERR or file_path == LOG_TO_STDOUT:
|
|
|
|
return True
|
2019-07-13 18:45:48 +00:00
|
|
|
if not file_path:
|
|
|
|
return True
|
|
|
|
if os.path.isdir(file_path):
|
|
|
|
return False
|
|
|
|
log_dir = os.path.dirname(file_path)
|
|
|
|
return (not log_dir) or os.path.isdir(log_dir)
|
|
|
|
|
|
|
|
|
|
|
|
def _absolute_log_file(log_file, default_log_file):
|
|
|
|
if log_file:
|
|
|
|
if not os.path.dirname(log_file):
|
2019-07-14 06:18:45 +00:00
|
|
|
log_file = os.path.join(_CONFIG_DIR, log_file)
|
2019-07-13 18:45:48 +00:00
|
|
|
return os.path.abspath(log_file)
|
|
|
|
return default_log_file
|
|
|
|
|
|
|
|
|
|
|
|
def get_logfile(log_file):
|
|
|
|
return _absolute_log_file(log_file, DEFAULT_LOG_FILE)
|
|
|
|
|
|
|
|
|
|
|
|
def get_accesslogfile(log_file):
|
|
|
|
return _absolute_log_file(log_file, DEFAULT_ACCESS_LOG)
|
|
|
|
|
|
|
|
|
|
|
|
def setup(log_file, log_level=None):
|
2022-05-21 20:52:59 +00:00
|
|
|
"""
|
2019-07-13 18:45:48 +00:00
|
|
|
Configure the logging output.
|
|
|
|
May be called multiple times.
|
2022-05-21 20:52:59 +00:00
|
|
|
"""
|
2019-07-14 11:43:40 +00:00
|
|
|
log_level = log_level or DEFAULT_LOG_LEVEL
|
2020-12-12 07:11:00 +00:00
|
|
|
logging.setLoggerClass(_Logger)
|
2019-07-14 11:43:40 +00:00
|
|
|
logging.getLogger(__package__).setLevel(log_level)
|
|
|
|
|
2019-07-13 18:45:48 +00:00
|
|
|
r = logging.root
|
2019-07-14 11:43:40 +00:00
|
|
|
if log_level >= logging.INFO or os.environ.get('FLASK_DEBUG'):
|
|
|
|
# avoid spamming the log with debug messages from libraries
|
|
|
|
r.setLevel(log_level)
|
2019-07-13 18:45:48 +00:00
|
|
|
|
2022-05-21 20:52:59 +00:00
|
|
|
# Otherwise, name gets destroyed on Windows
|
2020-04-19 17:08:58 +00:00
|
|
|
if log_file != LOG_TO_STDERR and log_file != LOG_TO_STDOUT:
|
2020-03-28 06:13:51 +00:00
|
|
|
log_file = _absolute_log_file(log_file, DEFAULT_LOG_FILE)
|
2019-07-31 06:40:36 +00:00
|
|
|
|
2019-07-13 18:45:48 +00:00
|
|
|
previous_handler = r.handlers[0] if r.handlers else None
|
|
|
|
if previous_handler:
|
|
|
|
# if the log_file has not changed, don't create a new handler
|
|
|
|
if getattr(previous_handler, 'baseFilename', None) == log_file:
|
2020-05-24 07:59:45 +00:00
|
|
|
return "" if log_file == DEFAULT_LOG_FILE else log_file
|
2019-07-14 11:43:40 +00:00
|
|
|
logging.debug("logging to %s level %s", log_file, r.level)
|
2019-07-13 18:45:48 +00:00
|
|
|
|
2019-11-16 09:09:34 +00:00
|
|
|
if log_file == LOG_TO_STDERR or log_file == LOG_TO_STDOUT:
|
|
|
|
if log_file == LOG_TO_STDOUT:
|
|
|
|
file_handler = StreamHandler(sys.stdout)
|
|
|
|
file_handler.baseFilename = log_file
|
|
|
|
else:
|
2020-03-28 06:13:51 +00:00
|
|
|
file_handler = StreamHandler(sys.stderr)
|
2019-11-16 09:09:34 +00:00
|
|
|
file_handler.baseFilename = log_file
|
2019-07-13 18:45:48 +00:00
|
|
|
else:
|
|
|
|
try:
|
2021-04-25 09:20:21 +00:00
|
|
|
file_handler = RotatingFileHandler(log_file, maxBytes=100000, backupCount=2, encoding='utf-8')
|
2023-11-02 15:34:04 +00:00
|
|
|
except (IOError, PermissionError):
|
2019-07-13 18:45:48 +00:00
|
|
|
if log_file == DEFAULT_LOG_FILE:
|
|
|
|
raise
|
2021-04-25 09:20:21 +00:00
|
|
|
file_handler = RotatingFileHandler(DEFAULT_LOG_FILE, maxBytes=100000, backupCount=2, encoding='utf-8')
|
2020-05-24 07:59:45 +00:00
|
|
|
log_file = ""
|
2019-07-13 18:45:48 +00:00
|
|
|
file_handler.setFormatter(FORMATTER)
|
|
|
|
|
|
|
|
for h in r.handlers:
|
|
|
|
r.removeHandler(h)
|
|
|
|
h.close()
|
|
|
|
r.addHandler(file_handler)
|
2022-05-09 17:47:43 +00:00
|
|
|
logging.captureWarnings(True)
|
2020-05-24 07:59:45 +00:00
|
|
|
return "" if log_file == DEFAULT_LOG_FILE else log_file
|
2019-07-13 18:45:48 +00:00
|
|
|
|
|
|
|
|
|
|
|
def create_access_log(log_file, log_name, formatter):
|
2022-05-21 20:52:59 +00:00
|
|
|
"""
|
2019-07-13 18:45:48 +00:00
|
|
|
One-time configuration for the web server's access log.
|
2022-05-21 20:52:59 +00:00
|
|
|
"""
|
2019-07-13 18:45:48 +00:00
|
|
|
log_file = _absolute_log_file(log_file, DEFAULT_ACCESS_LOG)
|
|
|
|
logging.debug("access log: %s", log_file)
|
|
|
|
|
|
|
|
access_log = logging.getLogger(log_name)
|
|
|
|
access_log.propagate = False
|
|
|
|
access_log.setLevel(logging.INFO)
|
2020-05-24 07:59:45 +00:00
|
|
|
try:
|
2020-08-26 19:43:58 +00:00
|
|
|
file_handler = RotatingFileHandler(log_file, maxBytes=50000, backupCount=2, encoding='utf-8')
|
2023-11-02 15:34:04 +00:00
|
|
|
except (IOError, PermissionError):
|
2020-05-24 07:59:45 +00:00
|
|
|
if log_file == DEFAULT_ACCESS_LOG:
|
|
|
|
raise
|
2020-08-26 19:43:58 +00:00
|
|
|
file_handler = RotatingFileHandler(DEFAULT_ACCESS_LOG, maxBytes=50000, backupCount=2, encoding='utf-8')
|
2020-05-25 19:30:21 +00:00
|
|
|
log_file = ""
|
2019-07-13 18:45:48 +00:00
|
|
|
|
|
|
|
file_handler.setFormatter(formatter)
|
|
|
|
access_log.addHandler(file_handler)
|
2022-05-21 20:52:59 +00:00
|
|
|
return access_log, "" if _absolute_log_file(log_file, DEFAULT_ACCESS_LOG) == DEFAULT_ACCESS_LOG else log_file
|
2019-07-13 18:45:48 +00:00
|
|
|
|
|
|
|
|
|
|
|
# Enable logging of smtp lib debug output
|
|
|
|
class StderrLogger(object):
|
|
|
|
def __init__(self, name=None):
|
|
|
|
self.log = get(name or self.__class__.__name__)
|
|
|
|
self.buffer = ''
|
|
|
|
|
|
|
|
def write(self, message):
|
|
|
|
try:
|
|
|
|
if message == '\n':
|
|
|
|
self.log.debug(self.buffer.replace('\n', '\\n'))
|
|
|
|
self.buffer = ''
|
|
|
|
else:
|
|
|
|
self.buffer += message
|
|
|
|
except Exception:
|
|
|
|
self.log.debug("Logging Error")
|
2019-07-31 06:40:36 +00:00
|
|
|
|
|
|
|
|
2019-11-16 09:09:34 +00:00
|
|
|
# default configuration, before application settings are applied
|
2019-07-31 06:40:36 +00:00
|
|
|
setup(LOG_TO_STDERR, logging.DEBUG if os.environ.get('FLASK_DEBUG') else DEFAULT_LOG_LEVEL)
|