summaryrefslogtreecommitdiff
path: root/raphodo/iplogging.py
blob: e58dd3d36ead04bc829fea31513efb2390cc3a47 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# Copyright (C) 2016 Damon Lynch <damonlynch@gmail.com>

# This file is part of Rapid Photo Downloader.
#
# Rapid Photo Downloader 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.
#
# Rapid Photo Downloader 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 Rapid Photo Downloader.  If not,
# see <http://www.gnu.org/licenses/>.

"""
Specify logging setup.

Log all messages to file log
Log messages at user specified level to console
"""

__author__ = 'Damon Lynch'
__copyright__ = "Copyright 2016, Damon Lynch"

import logging
from logging.handlers import QueueHandler, RotatingFileHandler
import pickle
import gzip
import os
from typing import Optional

try:
    import colorlog
    use_colorlog = True
except ImportError:
    use_colorlog = False

from raphodo.constants import logfile_name
from raphodo.storage import get_program_logging_directory

logging_format = '%(levelname)s: %(message)s'
colored_logging_format = '%(log_color)s%(levelname)-8s%(reset)s %(message)s'
log_colors ={
        'DEBUG':    'cyan',
        'INFO':     'green',
        'WARNING':  'yellow',
        'ERROR':    'red',
        'CRITICAL': 'red,bg_white',
    }

logging_date_format = '%Y-%m-%d %H:%M:%S'
file_logging_format = '%(asctime)s %(levelname)s %(filename)s %(lineno)d: %(message)s'

class ZeroMQSocketHandler(QueueHandler):
    def enqueue(self, record):
        data = pickle.dumps(record.__dict__)
        self.queue.send(data)

class RotatingGzipFileHandler(RotatingFileHandler):
    def rotation_filename(self, name):
        return name + ".gz"

    def rotate(self, source, dest):
        with open(source, "rb") as sf:
            with gzip.open(dest, "wb") as df:
                df.writelines(sf)
                os.remove(source)


def full_log_file_path():
    log_file_path = get_program_logging_directory(create_if_not_exist=True)
    if log_file_path is not None:
        log_file = os.path.join(log_file_path, logfile_name)
    else:
        # Problem: for some reason cannot create log file in standard location,
        # so create it in the home directory
        log_file = os.path.join(os.path.expanduser('~'), logfile_name)
    return log_file

def setup_main_process_logging(logging_level: int) -> logging.Logger:
    """
    Setup logging at the module level
    :param log_file_path: path where log file should be stored
    :param logging_level: logging module's logging level for console output
    :return: default logging object
    """

    log_file = full_log_file_path()
    logger = logging.getLogger()
    max_bytes = 1024 * 1024  # 1 MB
    filehandler = RotatingGzipFileHandler(log_file, maxBytes=max_bytes, backupCount=5)
    filehandler.setLevel(logging.DEBUG)
    filehandler.setFormatter(logging.Formatter(file_logging_format, logging_date_format))
    logger.addHandler(filehandler)
    logger.setLevel(logging.DEBUG)

    consolehandler = logging.StreamHandler()
    if not use_colorlog:
        consolehandler.setFormatter(logging.Formatter(logging_format))
    else:
        consolehandler.setFormatter(colorlog.ColoredFormatter(fmt=colored_logging_format,
                                                              log_colors=log_colors))
    consolehandler.setLevel(logging_level)
    logger.addHandler(consolehandler)
    return logger