«Книга рецептів» з логування

Автор:

Vinay Sajip <vinay_sajip at red-dove dot com>

This page contains a number of recipes related to logging, which have been found useful in the past. For links to tutorial and reference information, please see Інші ресурси.

Використання журналювання в кількох модулях

Кілька викликів logging.getLogger('someLogger') повертають посилання на той самий об’єкт журналу. Це вірно не лише в межах одного модуля, але й у всіх модулях, якщо вони знаходяться в одному процесі інтерпретатора Python. Це вірно для посилань на той самий об’єкт; крім того, код програми може визначати та налаштовувати батьківський реєстратор в одному модулі та створювати (але не налаштовувати) дочірній реєстратор в окремому модулі, і всі виклики реєстратора до дочірнього модуля передадуться батьківському. Ось головний модуль:

import logging
import auxiliary_module

# create logger with 'spam_application'
logger = logging.getLogger('spam_application')
logger.setLevel(logging.DEBUG)
# create file handler which logs even debug messages
fh = logging.FileHandler('spam.log')
fh.setLevel(logging.DEBUG)
# create console handler with a higher log level
ch = logging.StreamHandler()
ch.setLevel(logging.ERROR)
# create formatter and add it to the handlers
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
fh.setFormatter(formatter)
ch.setFormatter(formatter)
# add the handlers to the logger
logger.addHandler(fh)
logger.addHandler(ch)

logger.info('creating an instance of auxiliary_module.Auxiliary')
a = auxiliary_module.Auxiliary()
logger.info('created an instance of auxiliary_module.Auxiliary')
logger.info('calling auxiliary_module.Auxiliary.do_something')
a.do_something()
logger.info('finished auxiliary_module.Auxiliary.do_something')
logger.info('calling auxiliary_module.some_function()')
auxiliary_module.some_function()
logger.info('done with auxiliary_module.some_function()')

Ось допоміжний модуль:

import logging

# create logger
module_logger = logging.getLogger('spam_application.auxiliary')

class Auxiliary:
    def __init__(self):
        self.logger = logging.getLogger('spam_application.auxiliary.Auxiliary')
        self.logger.info('creating an instance of Auxiliary')

    def do_something(self):
        self.logger.info('doing something')
        a = 1 + 1
        self.logger.info('done doing something')

def some_function():
    module_logger.info('received a call to "some_function"')

Результат виглядає так:

2005-03-23 23:47:11,663 - spam_application - INFO -
   creating an instance of auxiliary_module.Auxiliary
2005-03-23 23:47:11,665 - spam_application.auxiliary.Auxiliary - INFO -
   creating an instance of Auxiliary
2005-03-23 23:47:11,665 - spam_application - INFO -
   created an instance of auxiliary_module.Auxiliary
2005-03-23 23:47:11,668 - spam_application - INFO -
   calling auxiliary_module.Auxiliary.do_something
2005-03-23 23:47:11,668 - spam_application.auxiliary.Auxiliary - INFO -
   doing something
2005-03-23 23:47:11,669 - spam_application.auxiliary.Auxiliary - INFO -
   done doing something
2005-03-23 23:47:11,670 - spam_application - INFO -
   finished auxiliary_module.Auxiliary.do_something
2005-03-23 23:47:11,671 - spam_application - INFO -
   calling auxiliary_module.some_function()
2005-03-23 23:47:11,672 - spam_application.auxiliary - INFO -
   received a call to 'some_function'
2005-03-23 23:47:11,673 - spam_application - INFO -
   done with auxiliary_module.some_function()

Ведення журналу з кількох потоків

Логування з кількох потоків не потребує особливих зусиль. У наступному прикладі показано журналювання з основного (початкового) потоку та іншого потоку:

import logging
import threading
import time

def worker(arg):
    while not arg['stop']:
        logging.debug('Hi from myfunc')
        time.sleep(0.5)

def main():
    logging.basicConfig(level=logging.DEBUG, format='%(relativeCreated)6d %(threadName)s %(message)s')
    info = {'stop': False}
    thread = threading.Thread(target=worker, args=(info,))
    thread.start()
    while True:
        try:
            logging.debug('Hello from main')
            time.sleep(0.75)
        except KeyboardInterrupt:
            info['stop'] = True
            break
    thread.join()

if __name__ == '__main__':
    main()

Під час запуску сценарій має надрукувати щось на зразок такого:

   0 Thread-1 Hi from myfunc
   3 MainThread Hello from main
 505 Thread-1 Hi from myfunc
 755 MainThread Hello from main
1007 Thread-1 Hi from myfunc
1507 MainThread Hello from main
1508 Thread-1 Hi from myfunc
2010 Thread-1 Hi from myfunc
2258 MainThread Hello from main
2512 Thread-1 Hi from myfunc
3009 MainThread Hello from main
3013 Thread-1 Hi from myfunc
3515 Thread-1 Hi from myfunc
3761 MainThread Hello from main
4017 Thread-1 Hi from myfunc
4513 MainThread Hello from main
4518 Thread-1 Hi from myfunc

Це показує результати журналювання, як і можна було очікувати. Звичайно, цей підхід працює для більшої кількості потоків, ніж показано тут.

Кілька обробників і форматувальників

Логери — це звичайні об’єкти Python. Метод addHandler() не має мінімальної або максимальної квоти на кількість обробників, які ви можете додати. Іноді для програми буде корисно реєструвати всі повідомлення будь-якого рівня серйозності в текстовий файл, одночасно реєструючи помилки або вище на консолі. Щоб налаштувати це, просто налаштуйте відповідні обробники. Виклики журналювання в коді програми залишаться незмінними. Ось невелика модифікація попереднього простого прикладу конфігурації на основі модуля:

import logging

logger = logging.getLogger('simple_example')
logger.setLevel(logging.DEBUG)
# create file handler which logs even debug messages
fh = logging.FileHandler('spam.log')
fh.setLevel(logging.DEBUG)
# create console handler with a higher log level
ch = logging.StreamHandler()
ch.setLevel(logging.ERROR)
# create formatter and add it to the handlers
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
ch.setFormatter(formatter)
fh.setFormatter(formatter)
# add the handlers to logger
logger.addHandler(ch)
logger.addHandler(fh)

# 'application' code
logger.debug('debug message')
logger.info('info message')
logger.warning('warn message')
logger.error('error message')
logger.critical('critical message')

Зауважте, що код програми не піклується про декілька обробників. Все, що змінилося, це додавання та налаштування нового обробника під назвою fh.

Можливість створювати нові обробники з фільтрами вищого або нижчого рівня серйозності може бути дуже корисною під час написання та тестування програми. Замість використання багатьох інструкцій print для налагодження використовуйте logger.debug: на відміну від інструкцій print, які вам доведеться видалити або закоментувати пізніше, інструкції logger.debug можуть залишатися недоторканими у вихідному коді. і залишаються в стані спокою, доки вони вам знову не знадобляться. У той час єдина зміна, яка має відбутися, це змінити рівень серйозності реєстратора та/або обробника для налагодження.

Реєстрація в кількох пунктах призначення

Припустімо, ви хочете увійти в консоль і файл із різними форматами повідомлень і за різних обставин. Скажімо, ви хочете реєструвати повідомлення з рівнями DEBUG і вище у файл, а ці повідомлення рівня INFO і вище — на консоль. Давайте також припустимо, що файл повинен містити мітки часу, але повідомлення консолі не повинні. Ось як ви можете цього досягти:

import logging

# set up logging to file - see previous section for more details
logging.basicConfig(level=logging.DEBUG,
                    format='%(asctime)s %(name)-12s %(levelname)-8s %(message)s',
                    datefmt='%m-%d %H:%M',
                    filename='/tmp/myapp.log',
                    filemode='w')
# define a Handler which writes INFO messages or higher to the sys.stderr
console = logging.StreamHandler()
console.setLevel(logging.INFO)
# set a format which is simpler for console use
formatter = logging.Formatter('%(name)-12s: %(levelname)-8s %(message)s')
# tell the handler to use this format
console.setFormatter(formatter)
# add the handler to the root logger
logging.getLogger('').addHandler(console)

# Now, we can log to the root logger, or any other logger. First the root...
logging.info('Jackdaws love my big sphinx of quartz.')

# Now, define a couple of other loggers which might represent areas in your
# application:

logger1 = logging.getLogger('myapp.area1')
logger2 = logging.getLogger('myapp.area2')

logger1.debug('Quick zephyrs blow, vexing daft Jim.')
logger1.info('How quickly daft jumping zebras vex.')
logger2.warning('Jail zesty vixen who grabbed pay from quack.')
logger2.error('The five boxing wizards jump quickly.')

Коли ви запустите це, на консолі ви побачите

root        : INFO     Jackdaws love my big sphinx of quartz.
myapp.area1 : INFO     How quickly daft jumping zebras vex.
myapp.area2 : WARNING  Jail zesty vixen who grabbed pay from quack.
myapp.area2 : ERROR    The five boxing wizards jump quickly.

і у файлі ви побачите щось на зразок

10-22 22:19 root         INFO     Jackdaws love my big sphinx of quartz.
10-22 22:19 myapp.area1  DEBUG    Quick zephyrs blow, vexing daft Jim.
10-22 22:19 myapp.area1  INFO     How quickly daft jumping zebras vex.
10-22 22:19 myapp.area2  WARNING  Jail zesty vixen who grabbed pay from quack.
10-22 22:19 myapp.area2  ERROR    The five boxing wizards jump quickly.

Як бачите, повідомлення DEBUG відображається лише у файлі. Інші повідомлення надсилаються в обидва адресати.

У цьому прикладі використовуються обробники консолі та файлів, але ви можете використовувати будь-яку кількість і комбінацію обробників на свій вибір.

Note that the above choice of log filename /tmp/myapp.log implies use of a standard location for temporary files on POSIX systems. On Windows, you may need to choose a different directory name for the log - just ensure that the directory exists and that you have the permissions to create and update files in it.

Custom handling of levels

Sometimes, you might want to do something slightly different from the standard handling of levels in handlers, where all levels above a threshold get processed by a handler. To do this, you need to use filters. Let’s look at a scenario where you want to arrange things as follows:

  • Send messages of severity INFO and WARNING to sys.stdout

  • Send messages of severity ERROR and above to sys.stderr

  • Send messages of severity DEBUG and above to file app.log

Suppose you configure logging with the following JSON:

{
    "version": 1,
    "disable_existing_loggers": false,
    "formatters": {
        "simple": {
            "format": "%(levelname)-8s - %(message)s"
        }
    },
    "handlers": {
        "stdout": {
            "class": "logging.StreamHandler",
            "level": "INFO",
            "formatter": "simple",
            "stream": "ext://sys.stdout"
        },
        "stderr": {
            "class": "logging.StreamHandler",
            "level": "ERROR",
            "formatter": "simple",
            "stream": "ext://sys.stderr"
        },
        "file": {
            "class": "logging.FileHandler",
            "formatter": "simple",
            "filename": "app.log",
            "mode": "w"
        }
    },
    "root": {
        "level": "DEBUG",
        "handlers": [
            "stderr",
            "stdout",
            "file"
        ]
    }
}

This configuration does almost what we want, except that sys.stdout would show messages of severity ERROR and above as well as INFO and WARNING messages. To prevent this, we can set up a filter which excludes those messages and add it to the relevant handler. This can be configured by adding a filters section parallel to formatters and handlers:

{
    "filters": {
        "warnings_and_below": {
            "()" : "__main__.filter_maker",
            "level": "WARNING"
        }
    }
}

and changing the section on the stdout handler to add it:

{
    "stdout": {
        "class": "logging.StreamHandler",
        "level": "INFO",
        "formatter": "simple",
        "stream": "ext://sys.stdout",
        "filters": ["warnings_and_below"]
    }
}

A filter is just a function, so we can define the filter_maker (a factory function) as follows:

def filter_maker(level):
    level = getattr(logging, level)

    def filter(record):
        return record.levelno <= level

    return filter

This converts the string argument passed in to a numeric level, and returns a function which only returns True if the level of the passed in record is at or below the specified level. Note that in this example I have defined the filter_maker in a test script main.py that I run from the command line, so its module will be __main__ - hence the __main__.filter_maker in the filter configuration. You will need to change that if you define it in a different module.

With the filter added, we can run main.py, which in full is:

import json
import logging
import logging.config

CONFIG = '''
{
    "version": 1,
    "disable_existing_loggers": false,
    "formatters": {
        "simple": {
            "format": "%(levelname)-8s - %(message)s"
        }
    },
    "filters": {
        "warnings_and_below": {
            "()" : "__main__.filter_maker",
            "level": "WARNING"
        }
    },
    "handlers": {
        "stdout": {
            "class": "logging.StreamHandler",
            "level": "INFO",
            "formatter": "simple",
            "stream": "ext://sys.stdout",
            "filters": ["warnings_and_below"]
        },
        "stderr": {
            "class": "logging.StreamHandler",
            "level": "ERROR",
            "formatter": "simple",
            "stream": "ext://sys.stderr"
        },
        "file": {
            "class": "logging.FileHandler",
            "formatter": "simple",
            "filename": "app.log",
            "mode": "w"
        }
    },
    "root": {
        "level": "DEBUG",
        "handlers": [
            "stderr",
            "stdout",
            "file"
        ]
    }
}
'''

def filter_maker(level):
    level = getattr(logging, level)

    def filter(record):
        return record.levelno <= level

    return filter

logging.config.dictConfig(json.loads(CONFIG))
logging.debug('A DEBUG message')
logging.info('An INFO message')
logging.warning('A WARNING message')
logging.error('An ERROR message')
logging.critical('A CRITICAL message')

And after running it like this:

python main.py 2>stderr.log >stdout.log

We can see the results are as expected:

$ more *.log
::::::::::::::
app.log
::::::::::::::
DEBUG    - A DEBUG message
INFO     - An INFO message
WARNING  - A WARNING message
ERROR    - An ERROR message
CRITICAL - A CRITICAL message
::::::::::::::
stderr.log
::::::::::::::
ERROR    - An ERROR message
CRITICAL - A CRITICAL message
::::::::::::::
stdout.log
::::::::::::::
INFO     - An INFO message
WARNING  - A WARNING message

Приклад конфігурації сервера

Ось приклад модуля, який використовує сервер конфігурації журналу:

import logging
import logging.config
import time
import os

# read initial config file
logging.config.fileConfig('logging.conf')

# create and start listener on port 9999
t = logging.config.listen(9999)
t.start()

logger = logging.getLogger('simpleExample')

try:
    # loop through logging calls to see the difference
    # new configurations make, until Ctrl+C is pressed
    while True:
        logger.debug('debug message')
        logger.info('info message')
        logger.warning('warn message')
        logger.error('error message')
        logger.critical('critical message')
        time.sleep(5)
except KeyboardInterrupt:
    # cleanup
    logging.config.stopListening()
    t.join()

А ось сценарій, який приймає ім’я файлу та надсилає цей файл на сервер, належним чином передуючи довжиною у двійковому кодуванні, як нову конфігурацію журналювання:

#!/usr/bin/env python
import socket, sys, struct

with open(sys.argv[1], 'rb') as f:
    data_to_send = f.read()

HOST = 'localhost'
PORT = 9999
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
print('connecting...')
s.connect((HOST, PORT))
print('sending config...')
s.send(struct.pack('>L', len(data_to_send)))
s.send(data_to_send)
s.close()
print('complete')

Робота з обробниками, які блокують

Іноді вам потрібно змусити обробників журналу виконувати свою роботу, не блокуючи потік, з якого ви входите. Це поширене явище у веб-додатках, хоча, звичайно, трапляється й в інших сценаріях.

Поширеним винуватцем млявої поведінки є SMTPHandler: надсилання електронних листів може тривати багато часу через ряд причин, які не залежать від розробника (наприклад, погана робота пошти чи мережевої інфраструктури). Але майже будь-який мережевий обробник може заблокувати: навіть операція SocketHandler може виконати DNS-запит під капотом, який надто повільний (і цей запит може бути глибоко в коді бібліотеки сокетів, нижче рівня Python, і поза вашим контролем).

Одним із рішень є використання підходу з двох частин. Для першої частини додайте лише QueueHandler до тих реєстраторів, доступ до яких здійснюється з критичних для продуктивності потоків. Вони просто записують у свою чергу, розмір якої може бути достатньо великим або ініціалізований без верхньої межі їхнього розміру. Запис до черги, як правило, швидко приймається, хоча вам, ймовірно, потрібно буде перехопити виняток queue.Full як запобіжний захід у вашому коді. Якщо ви розробник бібліотеки, у коді якого є критично важливі для продуктивності потоки, обов’язково задокументуйте це (разом із пропозицією приєднати лише QueueHandlers до ваших реєстраторів) на користь інших розробників, які використовуватимуть ваш код.

Другою частиною рішення є QueueListener, який був розроблений як відповідник QueueHandler. QueueListener дуже простий: він передає чергу та деякі обробники, і запускає внутрішній потік, який прослуховує свою чергу для LogRecords, надісланих з QueueHandlers (або будь-якого іншого джерела LogRecords, як на те пішло). LogRecords видаляються з черги та передаються обробникам для обробки.

Перевага наявності окремого класу QueueListener полягає в тому, що ви можете використовувати один екземпляр для обслуговування кількох QueueHandler. Це більш дружньо до ресурсів, ніж, скажімо, мати багатопотокові версії існуючих класів обробників, які з’їдають один потік на обробник без особливої користі.

Нижче наведено приклад використання цих двох класів (імпорт опущено):

que = queue.Queue(-1)  # no limit on size
queue_handler = QueueHandler(que)
handler = logging.StreamHandler()
listener = QueueListener(que, handler)
root = logging.getLogger()
root.addHandler(queue_handler)
formatter = logging.Formatter('%(threadName)s: %(message)s')
handler.setFormatter(formatter)
listener.start()
# The log output will display the thread which generated
# the event (the main thread) rather than the internal
# thread which monitors the internal queue. This is what
# you want to happen.
root.warning('Look out!')
listener.stop()

який під час запуску вироблятиме:

MainThread: Look out!

Примітка

Although the earlier discussion wasn’t specifically talking about async code, but rather about slow logging handlers, it should be noted that when logging from async code, network and even file handlers could lead to problems (blocking the event loop) because some logging is done from asyncio internals. It might be best, if any async code is used in an application, to use the above approach for logging, so that any blocking code runs only in the QueueListener thread.

Змінено в версії 3.5: До Python 3.5 QueueListener завжди передавав кожне повідомлення, отримане з черги, кожному обробнику, яким він був ініціалізований. (Це сталося тому, що передбачалося, що фільтрація рівня виконується з іншого боку, де заповнюється черга.) Починаючи з версії 3.5 і далі цю поведінку можна змінити, передавши аргумент ключового слова respect_handler_level=True конструктору слухача . Коли це зроблено, слухач порівнює рівень кожного повідомлення з рівнем обробника та передає повідомлення обробнику, лише якщо це доречно.

Надсилання та отримання журнальних подій через мережу

Припустімо, ви хочете надіслати події журналювання через мережу та обробити їх на приймаючому кінці. Простий спосіб зробити це — приєднати екземпляр SocketHandler до кореневого реєстратора на стороні надсилання:

import logging, logging.handlers

rootLogger = logging.getLogger('')
rootLogger.setLevel(logging.DEBUG)
socketHandler = logging.handlers.SocketHandler('localhost',
                    logging.handlers.DEFAULT_TCP_LOGGING_PORT)
# don't bother with a formatter, since a socket handler sends the event as
# an unformatted pickle
rootLogger.addHandler(socketHandler)

# Now, we can log to the root logger, or any other logger. First the root...
logging.info('Jackdaws love my big sphinx of quartz.')

# Now, define a couple of other loggers which might represent areas in your
# application:

logger1 = logging.getLogger('myapp.area1')
logger2 = logging.getLogger('myapp.area2')

logger1.debug('Quick zephyrs blow, vexing daft Jim.')
logger1.info('How quickly daft jumping zebras vex.')
logger2.warning('Jail zesty vixen who grabbed pay from quack.')
logger2.error('The five boxing wizards jump quickly.')

На стороні приймача ви можете налаштувати приймача за допомогою модуля socketserver. Ось базовий робочий приклад:

import pickle
import logging
import logging.handlers
import socketserver
import struct


class LogRecordStreamHandler(socketserver.StreamRequestHandler):
    """Handler for a streaming logging request.

    This basically logs the record using whatever logging policy is
    configured locally.
    """

    def handle(self):
        """
        Handle multiple requests - each expected to be a 4-byte length,
        followed by the LogRecord in pickle format. Logs the record
        according to whatever policy is configured locally.
        """
        while True:
            chunk = self.connection.recv(4)
            if len(chunk) < 4:
                break
            slen = struct.unpack('>L', chunk)[0]
            chunk = self.connection.recv(slen)
            while len(chunk) < slen:
                chunk = chunk + self.connection.recv(slen - len(chunk))
            obj = self.unPickle(chunk)
            record = logging.makeLogRecord(obj)
            self.handleLogRecord(record)

    def unPickle(self, data):
        return pickle.loads(data)

    def handleLogRecord(self, record):
        # if a name is specified, we use the named logger rather than the one
        # implied by the record.
        if self.server.logname is not None:
            name = self.server.logname
        else:
            name = record.name
        logger = logging.getLogger(name)
        # N.B. EVERY record gets logged. This is because Logger.handle
        # is normally called AFTER logger-level filtering. If you want
        # to do filtering, do it at the client end to save wasting
        # cycles and network bandwidth!
        logger.handle(record)

class LogRecordSocketReceiver(socketserver.ThreadingTCPServer):
    """
    Simple TCP socket-based logging receiver suitable for testing.
    """

    allow_reuse_address = True

    def __init__(self, host='localhost',
                 port=logging.handlers.DEFAULT_TCP_LOGGING_PORT,
                 handler=LogRecordStreamHandler):
        socketserver.ThreadingTCPServer.__init__(self, (host, port), handler)
        self.abort = 0
        self.timeout = 1
        self.logname = None

    def serve_until_stopped(self):
        import select
        abort = 0
        while not abort:
            rd, wr, ex = select.select([self.socket.fileno()],
                                       [], [],
                                       self.timeout)
            if rd:
                self.handle_request()
            abort = self.abort

def main():
    logging.basicConfig(
        format='%(relativeCreated)5d %(name)-15s %(levelname)-8s %(message)s')
    tcpserver = LogRecordSocketReceiver()
    print('About to start TCP server...')
    tcpserver.serve_until_stopped()

if __name__ == '__main__':
    main()

Спочатку запустіть сервер, а потім клієнт. На стороні клієнта на консолі нічого не друкується; на стороні сервера ви повинні побачити щось на кшталт:

About to start TCP server...
   59 root            INFO     Jackdaws love my big sphinx of quartz.
   59 myapp.area1     DEBUG    Quick zephyrs blow, vexing daft Jim.
   69 myapp.area1     INFO     How quickly daft jumping zebras vex.
   69 myapp.area2     WARNING  Jail zesty vixen who grabbed pay from quack.
   69 myapp.area2     ERROR    The five boxing wizards jump quickly.

Note that there are some security issues with pickle in some scenarios. If these affect you, you can use an alternative serialization scheme by overriding the makePickle() method and implementing your alternative there, as well as adapting the above script to use your alternative serialization.

Запуск прослуховувача сокетів журналювання у виробництві

To run a logging listener in production, you may need to use a process-management tool such as Supervisor. Here is a Gist which provides the bare-bones files to run the above functionality using Supervisor. It consists of the following files:

File

призначення

prepare.sh

A Bash script to prepare the environment for testing

supervisor.conf

The Supervisor configuration file, which has entries for the listener and a multi-process web application

ensure_app.sh

A Bash script to ensure that Supervisor is running with the above configuration

log_listener.py

The socket listener program which receives log events and records them to a file

main.py

A simple web application which performs logging via a socket connected to the listener

webapp.json

A JSON configuration file for the web application

client.py

A Python script to exercise the web application

The web application uses Gunicorn, which is a popular web application server that starts multiple worker processes to handle requests. This example setup shows how the workers can write to the same log file without conflicting with one another — they all go through the socket listener.

To test these files, do the following in a POSIX environment:

  1. Download the Gist as a ZIP archive using the Download ZIP button.

  2. Unzip the above files from the archive into a scratch directory.

  3. In the scratch directory, run bash prepare.sh to get things ready. This creates a run subdirectory to contain Supervisor-related and log files, and a venv subdirectory to contain a virtual environment into which bottle, gunicorn and supervisor are installed.

  4. Run bash ensure_app.sh to ensure that Supervisor is running with the above configuration.

  5. Run venv/bin/python client.py to exercise the web application, which will lead to records being written to the log.

  6. Inspect the log files in the run subdirectory. You should see the most recent log lines in files matching the pattern app.log*. They won’t be in any particular order, since they have been handled concurrently by different worker processes in a non-deterministic way.

  7. You can shut down the listener and the web application by running venv/bin/supervisorctl -c supervisor.conf shutdown.

You may need to tweak the configuration files in the unlikely event that the configured ports clash with something else in your test environment.

Додавання контекстної інформації до вихідних даних журналу

Іноді потрібно, щоб вихід журналу містив контекстну інформацію на додаток до параметрів, переданих до виклику журналу. Наприклад, у мережевій програмі може бути бажаним реєструвати інформацію про клієнта в журналі (наприклад, ім’я користувача віддаленого клієнта або IP-адресу). Хоча для цього можна використовувати параметр extra, не завжди зручно передавати інформацію таким чином. Хоча може виникнути спокуса створити екземпляри Logger для кожного з’єднання, це не дуже гарна ідея, оскільки ці екземпляри не збираються для сміття. Хоча це не є проблемою на практиці, коли кількість екземплярів Logger залежить від рівня деталізації, який ви хочете використовувати для реєстрації програми, може бути важко керувати кількістю Екземпляри Logger стають фактично необмеженими.

Використання LoggerAdapters для передачі контекстної інформації

Простий спосіб, за допомогою якого ви можете передати контекстну інформацію для виведення разом із інформацією про подію журналювання, полягає у використанні класу LoggerAdapter. Цей клас розроблено так, щоб виглядати як Logger, щоб ви могли викликати debug(), info(), warning(), error(), exception(), critical() і log(). Ці методи мають ті самі сигнатури, що й їхні аналоги в Logger, тому ви можете використовувати обидва типи екземплярів як взаємозамінні.

Коли ви створюєте екземпляр LoggerAdapter, ви передаєте йому екземпляр Logger і dict-подібний об’єкт, який містить вашу контекстну інформацію. Коли ви викликаєте один із методів журналювання в екземплярі LoggerAdapter, він делегує виклик базовому екземпляру Logger, який передається його конструктору, і організовує передачу контекстної інформації в делегованому виклику . Ось фрагмент коду LoggerAdapter:

def debug(self, msg, /, *args, **kwargs):
    """
    Delegate a debug call to the underlying logger, after adding
    contextual information from this adapter instance.
    """
    msg, kwargs = self.process(msg, kwargs)
    self.logger.debug(msg, *args, **kwargs)

У методі process() LoggerAdapter контекстна інформація додається до результатів журналювання. Він передає аргументи повідомлення та ключове слово виклику журналювання, а також повертає (потенційно) модифіковані версії їх для використання у виклику базовому реєстратору. Реалізація цього методу за замовчуванням залишає повідомлення в спокої, але вставляє «додатковий» ключ в аргумент ключового слова, значенням якого є dict-подібний об’єкт, переданий конструктору. Звичайно, якщо ви передали «додатковий» аргумент ключового слова під час виклику адаптера, він буде мовчки перезаписаний.

Перевага використання «extra» полягає в тому, що значення в dict-подібному об’єкті об’єднуються в __dict__ екземпляра LogRecord, що дозволяє вам використовувати налаштовані рядки з вашими екземплярами Formatter, які знають про ключі диктоподібного об’єкта. Якщо вам потрібен інший метод, напр. якщо ви хочете передати або додати контекстну інформацію до рядка повідомлення, вам просто потрібно створити підклас LoggerAdapter і перевизначити process(), щоб зробити те, що вам потрібно. Ось простий приклад:

class CustomAdapter(logging.LoggerAdapter):
    """
    This example adapter expects the passed in dict-like object to have a
    'connid' key, whose value in brackets is prepended to the log message.
    """
    def process(self, msg, kwargs):
        return '[%s] %s' % (self.extra['connid'], msg), kwargs

який можна використовувати таким чином:

logger = logging.getLogger(__name__)
adapter = CustomAdapter(logger, {'connid': some_conn_id})

Тоді будь-які події, які ви реєструєте в адаптері, матимуть значення some_conn_id до повідомлень журналу.

Використання об’єктів, відмінних від dicts, для передачі контекстної інформації

Вам не потрібно передавати фактичний dict до LoggerAdapter - ви можете передати примірник класу, який реалізує __getitem__ і __iter__, щоб він виглядав як dict для журналювання. Це буде корисно, якщо ви хочете генерувати значення динамічно (тоді як значення в dict будуть постійними).

Використання фільтрів для передачі контекстної інформації

Ви також можете додати контекстну інформацію до виведення журналу за допомогою визначеного користувачем Filter. Екземплярам Filter дозволено змінювати LogRecords, передані їм, включаючи додавання додаткових атрибутів, які потім можуть бути виведені за допомогою рядка відповідного формату або, якщо необхідно, спеціального Formatter.

Наприклад, у веб-додатку запит, який обробляється (або, принаймні, його цікаві частини) можна зберегти у змінній threadlocal (threading.local), а потім отримати доступ із Фільтра щоб додати, скажімо, інформацію із запиту - скажімо, віддалену IP-адресу та ім’я користувача віддаленого користувача - до LogRecord, використовуючи імена атрибутів „ip“ і „user“, як у прикладі LoggerAdapter вище . У цьому випадку можна використати той самий рядок формату, щоб отримати вихід, схожий на показаний вище. Ось приклад сценарію:

import logging
from random import choice

class ContextFilter(logging.Filter):
    """
    This is a filter which injects contextual information into the log.

    Rather than use actual contextual information, we just use random
    data in this demo.
    """

    USERS = ['jim', 'fred', 'sheila']
    IPS = ['123.231.231.123', '127.0.0.1', '192.168.0.1']

    def filter(self, record):

        record.ip = choice(ContextFilter.IPS)
        record.user = choice(ContextFilter.USERS)
        return True

if __name__ == '__main__':
    levels = (logging.DEBUG, logging.INFO, logging.WARNING, logging.ERROR, logging.CRITICAL)
    logging.basicConfig(level=logging.DEBUG,
                        format='%(asctime)-15s %(name)-5s %(levelname)-8s IP: %(ip)-15s User: %(user)-8s %(message)s')
    a1 = logging.getLogger('a.b.c')
    a2 = logging.getLogger('d.e.f')

    f = ContextFilter()
    a1.addFilter(f)
    a2.addFilter(f)
    a1.debug('A debug message')
    a1.info('An info message with %s', 'some parameters')
    for x in range(10):
        lvl = choice(levels)
        lvlname = logging.getLevelName(lvl)
        a2.log(lvl, 'A message at %s level with %d %s', lvlname, 2, 'parameters')

який під час запуску створює щось на зразок:

2010-09-06 22:38:15,292 a.b.c DEBUG    IP: 123.231.231.123 User: fred     A debug message
2010-09-06 22:38:15,300 a.b.c INFO     IP: 192.168.0.1     User: sheila   An info message with some parameters
2010-09-06 22:38:15,300 d.e.f CRITICAL IP: 127.0.0.1       User: sheila   A message at CRITICAL level with 2 parameters
2010-09-06 22:38:15,300 d.e.f ERROR    IP: 127.0.0.1       User: jim      A message at ERROR level with 2 parameters
2010-09-06 22:38:15,300 d.e.f DEBUG    IP: 127.0.0.1       User: sheila   A message at DEBUG level with 2 parameters
2010-09-06 22:38:15,300 d.e.f ERROR    IP: 123.231.231.123 User: fred     A message at ERROR level with 2 parameters
2010-09-06 22:38:15,300 d.e.f CRITICAL IP: 192.168.0.1     User: jim      A message at CRITICAL level with 2 parameters
2010-09-06 22:38:15,300 d.e.f CRITICAL IP: 127.0.0.1       User: sheila   A message at CRITICAL level with 2 parameters
2010-09-06 22:38:15,300 d.e.f DEBUG    IP: 192.168.0.1     User: jim      A message at DEBUG level with 2 parameters
2010-09-06 22:38:15,301 d.e.f ERROR    IP: 127.0.0.1       User: sheila   A message at ERROR level with 2 parameters
2010-09-06 22:38:15,301 d.e.f DEBUG    IP: 123.231.231.123 User: fred     A message at DEBUG level with 2 parameters
2010-09-06 22:38:15,301 d.e.f INFO     IP: 123.231.231.123 User: fred     A message at INFO level with 2 parameters

Use of contextvars

Since Python 3.7, the contextvars module has provided context-local storage which works for both threading and asyncio processing needs. This type of storage may thus be generally preferable to thread-locals. The following example shows how, in a multi-threaded environment, logs can populated with contextual information such as, for example, request attributes handled by web applications.

For the purposes of illustration, say that you have different web applications, each independent of the other but running in the same Python process and using a library common to them. How can each of these applications have their own log, where all logging messages from the library (and other request processing code) are directed to the appropriate application’s log file, while including in the log additional contextual information such as client IP, HTTP request method and client username?

Let’s assume that the library can be simulated by the following code:

# webapplib.py
import logging
import time

logger = logging.getLogger(__name__)

def useful():
    # Just a representative event logged from the library
    logger.debug('Hello from webapplib!')
    # Just sleep for a bit so other threads get to run
    time.sleep(0.01)

We can simulate the multiple web applications by means of two simple classes, Request and WebApp. These simulate how real threaded web applications work - each request is handled by a thread:

# main.py
import argparse
from contextvars import ContextVar
import logging
import os
from random import choice
import threading
import webapplib

logger = logging.getLogger(__name__)
root = logging.getLogger()
root.setLevel(logging.DEBUG)

class Request:
    """
    A simple dummy request class which just holds dummy HTTP request method,
    client IP address and client username
    """
    def __init__(self, method, ip, user):
        self.method = method
        self.ip = ip
        self.user = user

# A dummy set of requests which will be used in the simulation - we'll just pick
# from this list randomly. Note that all GET requests are from 192.168.2.XXX
# addresses, whereas POST requests are from 192.16.3.XXX addresses. Three users
# are represented in the sample requests.

REQUESTS = [
    Request('GET', '192.168.2.20', 'jim'),
    Request('POST', '192.168.3.20', 'fred'),
    Request('GET', '192.168.2.21', 'sheila'),
    Request('POST', '192.168.3.21', 'jim'),
    Request('GET', '192.168.2.22', 'fred'),
    Request('POST', '192.168.3.22', 'sheila'),
]

# Note that the format string includes references to request context information
# such as HTTP method, client IP and username

formatter = logging.Formatter('%(threadName)-11s %(appName)s %(name)-9s %(user)-6s %(ip)s %(method)-4s %(message)s')

# Create our context variables. These will be filled at the start of request
# processing, and used in the logging that happens during that processing

ctx_request = ContextVar('request')
ctx_appname = ContextVar('appname')

class InjectingFilter(logging.Filter):
    """
    A filter which injects context-specific information into logs and ensures
    that only information for a specific webapp is included in its log
    """
    def __init__(self, app):
        self.app = app

    def filter(self, record):
        request = ctx_request.get()
        record.method = request.method
        record.ip = request.ip
        record.user = request.user
        record.appName = appName = ctx_appname.get()
        return appName == self.app.name

class WebApp:
    """
    A dummy web application class which has its own handler and filter for a
    webapp-specific log.
    """
    def __init__(self, name):
        self.name = name
        handler = logging.FileHandler(name + '.log', 'w')
        f = InjectingFilter(self)
        handler.setFormatter(formatter)
        handler.addFilter(f)
        root.addHandler(handler)
        self.num_requests = 0

    def process_request(self, request):
        """
        This is the dummy method for processing a request. It's called on a
        different thread for every request. We store the context information into
        the context vars before doing anything else.
        """
        ctx_request.set(request)
        ctx_appname.set(self.name)
        self.num_requests += 1
        logger.debug('Request processing started')
        webapplib.useful()
        logger.debug('Request processing finished')

def main():
    fn = os.path.splitext(os.path.basename(__file__))[0]
    adhf = argparse.ArgumentDefaultsHelpFormatter
    ap = argparse.ArgumentParser(formatter_class=adhf, prog=fn,
                                 description='Simulate a couple of web '
                                             'applications handling some '
                                             'requests, showing how request '
                                             'context can be used to '
                                             'populate logs')
    aa = ap.add_argument
    aa('--count', '-c', type=int, default=100, help='How many requests to simulate')
    options = ap.parse_args()

    # Create the dummy webapps and put them in a list which we can use to select
    # from randomly
    app1 = WebApp('app1')
    app2 = WebApp('app2')
    apps = [app1, app2]
    threads = []
    # Add a common handler which will capture all events
    handler = logging.FileHandler('app.log', 'w')
    handler.setFormatter(formatter)
    root.addHandler(handler)

    # Generate calls to process requests
    for i in range(options.count):
        try:
            # Pick an app at random and a request for it to process
            app = choice(apps)
            request = choice(REQUESTS)
            # Process the request in its own thread
            t = threading.Thread(target=app.process_request, args=(request,))
            threads.append(t)
            t.start()
        except KeyboardInterrupt:
            break

    # Wait for the threads to terminate
    for t in threads:
        t.join()

    for app in apps:
        print('%s processed %s requests' % (app.name, app.num_requests))

if __name__ == '__main__':
    main()

If you run the above, you should find that roughly half the requests go into app1.log and the rest into app2.log, and the all the requests are logged to app.log. Each webapp-specific log will contain only log entries for only that webapp, and the request information will be displayed consistently in the log (i.e. the information in each dummy request will always appear together in a log line). This is illustrated by the following shell output:

~/logging-contextual-webapp$ python main.py
app1 processed 51 requests
app2 processed 49 requests
~/logging-contextual-webapp$ wc -l *.log
  153 app1.log
  147 app2.log
  300 app.log
  600 total
~/logging-contextual-webapp$ head -3 app1.log
Thread-3 (process_request) app1 __main__  jim    192.168.3.21 POST Request processing started
Thread-3 (process_request) app1 webapplib jim    192.168.3.21 POST Hello from webapplib!
Thread-5 (process_request) app1 __main__  jim    192.168.3.21 POST Request processing started
~/logging-contextual-webapp$ head -3 app2.log
Thread-1 (process_request) app2 __main__  sheila 192.168.2.21 GET  Request processing started
Thread-1 (process_request) app2 webapplib sheila 192.168.2.21 GET  Hello from webapplib!
Thread-2 (process_request) app2 __main__  jim    192.168.2.20 GET  Request processing started
~/logging-contextual-webapp$ head app.log
Thread-1 (process_request) app2 __main__  sheila 192.168.2.21 GET  Request processing started
Thread-1 (process_request) app2 webapplib sheila 192.168.2.21 GET  Hello from webapplib!
Thread-2 (process_request) app2 __main__  jim    192.168.2.20 GET  Request processing started
Thread-3 (process_request) app1 __main__  jim    192.168.3.21 POST Request processing started
Thread-2 (process_request) app2 webapplib jim    192.168.2.20 GET  Hello from webapplib!
Thread-3 (process_request) app1 webapplib jim    192.168.3.21 POST Hello from webapplib!
Thread-4 (process_request) app2 __main__  fred   192.168.2.22 GET  Request processing started
Thread-5 (process_request) app1 __main__  jim    192.168.3.21 POST Request processing started
Thread-4 (process_request) app2 webapplib fred   192.168.2.22 GET  Hello from webapplib!
Thread-6 (process_request) app1 __main__  jim    192.168.3.21 POST Request processing started
~/logging-contextual-webapp$ grep app1 app1.log | wc -l
153
~/logging-contextual-webapp$ grep app2 app2.log | wc -l
147
~/logging-contextual-webapp$ grep app1 app.log | wc -l
153
~/logging-contextual-webapp$ grep app2 app.log | wc -l
147

Imparting contextual information in handlers

Each Handler has its own chain of filters. If you want to add contextual information to a LogRecord without leaking it to other handlers, you can use a filter that returns a new LogRecord instead of modifying it in-place, as shown in the following script:

import copy
import logging

def filter(record: logging.LogRecord):
    record = copy.copy(record)
    record.user = 'jim'
    return record

if __name__ == '__main__':
    logger = logging.getLogger()
    logger.setLevel(logging.INFO)
    handler = logging.StreamHandler()
    formatter = logging.Formatter('%(message)s from %(user)-8s')
    handler.setFormatter(formatter)
    handler.addFilter(filter)
    logger.addHandler(handler)

    logger.info('A log message')

Реєстрація в один файл з кількох процесів

Хоча ведення журналу є потокобезпечним і підтримується журналювання в один файл із кількох потоків в одному процесі, журналювання в один файл із кількох процесів *не підтримується, оскільки немає стандартного способу серіалізації доступу в один файл через кілька процесів у Python. Якщо вам потрібно ввійти до одного файлу з кількох процесів, один із способів зробити це — зареєструвати всі процеси в SocketHandler і мати окремий процес, який реалізує сервер сокетів, який читає з сокет і журнали в файл. (Якщо ви віддаєте перевагу, ви можете виділити один потік в одному з існуючих процесів для виконання цієї функції.) Цей розділ документує цей підхід більш детально та містить робочий приймач сокетів, який можна використовувати як відправну точку для адаптації у власних програмах.

Ви також можете написати власний обробник, який використовує клас Lock з модуля multiprocessing для серіалізації доступу до файлу з ваших процесів. Існуючий FileHandler і підкласи не використовують multiprocessing наразі, хоча вони можуть використовувати це в майбутньому. Зауважте, що наразі модуль multiprocessing не забезпечує робочу функцію блокування на всіх платформах (див. https://bugs.python.org/issue3770).

Крім того, ви можете використовувати Queue і QueueHandler, щоб надсилати всі події журналювання до одного з процесів у вашій багатопроцесовій програмі. Наступний приклад сценарію демонструє, як це можна зробити; у прикладі окремий процес слухача прослуховує події, надіслані іншими процесами, і реєструє їх відповідно до власної конфігурації журналювання. Хоча приклад демонструє лише один спосіб зробити це (наприклад, ви можете використовувати потік слухача, а не окремий процес слухача — реалізація буде аналогічною), він дозволяє абсолютно різні конфігурації журналювання для слухача та інших процеси у вашій програмі та можуть бути використані як основа для коду, який відповідає вашим власним вимогам:

# You'll need these imports in your own code
import logging
import logging.handlers
import multiprocessing

# Next two import lines for this demo only
from random import choice, random
import time

#
# Because you'll want to define the logging configurations for listener and workers, the
# listener and worker process functions take a configurer parameter which is a callable
# for configuring logging for that process. These functions are also passed the queue,
# which they use for communication.
#
# In practice, you can configure the listener however you want, but note that in this
# simple example, the listener does not apply level or filter logic to received records.
# In practice, you would probably want to do this logic in the worker processes, to avoid
# sending events which would be filtered out between processes.
#
# The size of the rotated files is made small so you can see the results easily.
def listener_configurer():
    root = logging.getLogger()
    h = logging.handlers.RotatingFileHandler('mptest.log', 'a', 300, 10)
    f = logging.Formatter('%(asctime)s %(processName)-10s %(name)s %(levelname)-8s %(message)s')
    h.setFormatter(f)
    root.addHandler(h)

# This is the listener process top-level loop: wait for logging events
# (LogRecords)on the queue and handle them, quit when you get a None for a
# LogRecord.
def listener_process(queue, configurer):
    configurer()
    while True:
        try:
            record = queue.get()
            if record is None:  # We send this as a sentinel to tell the listener to quit.
                break
            logger = logging.getLogger(record.name)
            logger.handle(record)  # No level or filter logic applied - just do it!
        except Exception:
            import sys, traceback
            print('Whoops! Problem:', file=sys.stderr)
            traceback.print_exc(file=sys.stderr)

# Arrays used for random selections in this demo

LEVELS = [logging.DEBUG, logging.INFO, logging.WARNING,
          logging.ERROR, logging.CRITICAL]

LOGGERS = ['a.b.c', 'd.e.f']

MESSAGES = [
    'Random message #1',
    'Random message #2',
    'Random message #3',
]

# The worker configuration is done at the start of the worker process run.
# Note that on Windows you can't rely on fork semantics, so each process
# will run the logging configuration code when it starts.
def worker_configurer(queue):
    h = logging.handlers.QueueHandler(queue)  # Just the one handler needed
    root = logging.getLogger()
    root.addHandler(h)
    # send all messages, for demo; no other level or filter logic applied.
    root.setLevel(logging.DEBUG)

# This is the worker process top-level loop, which just logs ten events with
# random intervening delays before terminating.
# The print messages are just so you know it's doing something!
def worker_process(queue, configurer):
    configurer(queue)
    name = multiprocessing.current_process().name
    print('Worker started: %s' % name)
    for i in range(10):
        time.sleep(random())
        logger = logging.getLogger(choice(LOGGERS))
        level = choice(LEVELS)
        message = choice(MESSAGES)
        logger.log(level, message)
    print('Worker finished: %s' % name)

# Here's where the demo gets orchestrated. Create the queue, create and start
# the listener, create ten workers and start them, wait for them to finish,
# then send a None to the queue to tell the listener to finish.
def main():
    queue = multiprocessing.Queue(-1)
    listener = multiprocessing.Process(target=listener_process,
                                       args=(queue, listener_configurer))
    listener.start()
    workers = []
    for i in range(10):
        worker = multiprocessing.Process(target=worker_process,
                                         args=(queue, worker_configurer))
        workers.append(worker)
        worker.start()
    for w in workers:
        w.join()
    queue.put_nowait(None)
    listener.join()

if __name__ == '__main__':
    main()

Варіант наведеного вище сценарію зберігає журнали в основному процесі в окремому потоці:

import logging
import logging.config
import logging.handlers
from multiprocessing import Process, Queue
import random
import threading
import time

def logger_thread(q):
    while True:
        record = q.get()
        if record is None:
            break
        logger = logging.getLogger(record.name)
        logger.handle(record)


def worker_process(q):
    qh = logging.handlers.QueueHandler(q)
    root = logging.getLogger()
    root.setLevel(logging.DEBUG)
    root.addHandler(qh)
    levels = [logging.DEBUG, logging.INFO, logging.WARNING, logging.ERROR,
              logging.CRITICAL]
    loggers = ['foo', 'foo.bar', 'foo.bar.baz',
               'spam', 'spam.ham', 'spam.ham.eggs']
    for i in range(100):
        lvl = random.choice(levels)
        logger = logging.getLogger(random.choice(loggers))
        logger.log(lvl, 'Message no. %d', i)

if __name__ == '__main__':
    q = Queue()
    d = {
        'version': 1,
        'formatters': {
            'detailed': {
                'class': 'logging.Formatter',
                'format': '%(asctime)s %(name)-15s %(levelname)-8s %(processName)-10s %(message)s'
            }
        },
        'handlers': {
            'console': {
                'class': 'logging.StreamHandler',
                'level': 'INFO',
            },
            'file': {
                'class': 'logging.FileHandler',
                'filename': 'mplog.log',
                'mode': 'w',
                'formatter': 'detailed',
            },
            'foofile': {
                'class': 'logging.FileHandler',
                'filename': 'mplog-foo.log',
                'mode': 'w',
                'formatter': 'detailed',
            },
            'errors': {
                'class': 'logging.FileHandler',
                'filename': 'mplog-errors.log',
                'mode': 'w',
                'level': 'ERROR',
                'formatter': 'detailed',
            },
        },
        'loggers': {
            'foo': {
                'handlers': ['foofile']
            }
        },
        'root': {
            'level': 'DEBUG',
            'handlers': ['console', 'file', 'errors']
        },
    }
    workers = []
    for i in range(5):
        wp = Process(target=worker_process, name='worker %d' % (i + 1), args=(q,))
        workers.append(wp)
        wp.start()
    logging.config.dictConfig(d)
    lp = threading.Thread(target=logger_thread, args=(q,))
    lp.start()
    # At this point, the main process could do some useful work of its own
    # Once it's done that, it can wait for the workers to terminate...
    for wp in workers:
        wp.join()
    # And now tell the logging thread to finish up, too
    q.put(None)
    lp.join()

Цей варіант показує, як ви можете напр. застосувати конфігурацію для певних реєстраторів - напр. Логер foo має спеціальний обробник, який зберігає всі події в підсистемі foo у файлі mplog-foo.log. Це використовуватиметься механізмом реєстрації в основному процесі (навіть якщо події журналювання генеруються в робочих процесах), щоб спрямувати повідомлення до відповідних адресатів.

Використання concurrent.futures.ProcessPoolExecutor

Якщо ви хочете використовувати concurrent.futures.ProcessPoolExecutor для запуску ваших робочих процесів, вам потрібно створити чергу трохи інакше. Замість

queue = multiprocessing.Queue(-1)

ви повинні використовувати

queue = multiprocessing.Manager().Queue(-1)  # also works with the examples above

а потім ви можете замінити робоче створення з цього:

workers = []
for i in range(10):
    worker = multiprocessing.Process(target=worker_process,
                                     args=(queue, worker_configurer))
    workers.append(worker)
    worker.start()
for w in workers:
    w.join()

до цього (не забувши спочатку імпортувати concurrent.futures):

with concurrent.futures.ProcessPoolExecutor(max_workers=10) as executor:
    for i in range(10):
        executor.submit(worker_process, queue, worker_configurer)

Розгортання веб-додатків за допомогою Gunicorn і uWSGI

Під час розгортання веб-програм за допомогою Gunicorn або uWSGI (або подібних), для обробки запитів клієнтів створюється кілька робочих процесів. У таких середовищах уникайте створення обробників на основі файлів безпосередньо у вашій веб-програмі. Замість цього використовуйте SocketHandler, щоб увійти з веб-програми до слухача в окремому процесі. Це можна налаштувати за допомогою інструменту керування процесами, такого як Supervisor - див. Running a logging socket listener in production для отримання додаткової інформації.

Використання ротації файлів

Sometimes you want to let a log file grow to a certain size, then open a new file and log to that. You may want to keep a certain number of these files, and when that many files have been created, rotate the files so that the number of files and the size of the files both remain bounded. For this usage pattern, the logging package provides a RotatingFileHandler:

import glob
import logging
import logging.handlers

LOG_FILENAME = 'logging_rotatingfile_example.out'

# Set up a specific logger with our desired output level
my_logger = logging.getLogger('MyLogger')
my_logger.setLevel(logging.DEBUG)

# Add the log message handler to the logger
handler = logging.handlers.RotatingFileHandler(
              LOG_FILENAME, maxBytes=20, backupCount=5)

my_logger.addHandler(handler)

# Log some messages
for i in range(20):
    my_logger.debug('i = %d' % i)

# See what files are created
logfiles = glob.glob('%s*' % LOG_FILENAME)

for filename in logfiles:
    print(filename)

У результаті повинно вийти 6 окремих файлів, кожен з яких містить частину журналу журналу програми:

logging_rotatingfile_example.out
logging_rotatingfile_example.out.1
logging_rotatingfile_example.out.2
logging_rotatingfile_example.out.3
logging_rotatingfile_example.out.4
logging_rotatingfile_example.out.5

Найновішим файлом завжди є logging_rotatingfile_example.out, і кожного разу, коли він досягає ліміту розміру, він перейменовується з суфіксом .1. Кожен із наявних файлів резервної копії перейменовується, щоб збільшити суфікс (.1 стає .2 тощо), а файл .6 видаляється.

Очевидно, що цей приклад встановлює занадто малу довжину журналу як екстремальний приклад. Вам потрібно встановити відповідне значення maxBytes.

Використання альтернативних стилів форматування

Коли журналювання було додано до стандартної бібліотеки Python, єдиним способом форматування повідомлень зі змінним вмістом було використання методу %-formatting. Відтоді Python отримав два нові підходи до форматування: string.Template (доданий у Python 2.4) і str.format() (доданий у Python 2.6).

Журналування (станом на 3.2) забезпечує покращену підтримку цих двох додаткових стилів форматування. Клас Formatter було вдосконалено, щоб прийняти додатковий необов’язковий параметр ключового слова під назвою style. За замовчуванням це '%', але інші можливі значення '{' і '$', які відповідають двом іншим стилям форматування. Зворотна сумісність підтримується за замовчуванням (як і слід було очікувати), але, явно вказавши параметр стилю, ви отримуєте можливість вказати рядки формату, які працюють із str.format() або string.Template. Ось приклад сеансу консолі, щоб показати можливості:

>>> import logging
>>> root = logging.getLogger()
>>> root.setLevel(logging.DEBUG)
>>> handler = logging.StreamHandler()
>>> bf = logging.Formatter('{asctime} {name} {levelname:8s} {message}',
...                        style='{')
>>> handler.setFormatter(bf)
>>> root.addHandler(handler)
>>> logger = logging.getLogger('foo.bar')
>>> logger.debug('This is a DEBUG message')
2010-10-28 15:11:55,341 foo.bar DEBUG    This is a DEBUG message
>>> logger.critical('This is a CRITICAL message')
2010-10-28 15:12:11,526 foo.bar CRITICAL This is a CRITICAL message
>>> df = logging.Formatter('$asctime $name ${levelname} $message',
...                        style='$')
>>> handler.setFormatter(df)
>>> logger.debug('This is a DEBUG message')
2010-10-28 15:13:06,924 foo.bar DEBUG This is a DEBUG message
>>> logger.critical('This is a CRITICAL message')
2010-10-28 15:13:11,494 foo.bar CRITICAL This is a CRITICAL message
>>>

Зверніть увагу, що форматування повідомлень журналу для остаточного виведення в журнали повністю не залежить від того, як побудовано окреме повідомлення журналу. Це все ще може використовувати %-formatting, як показано тут:

>>> logger.error('This is an%s %s %s', 'other,', 'ERROR,', 'message')
2010-10-28 15:19:29,833 foo.bar ERROR This is another, ERROR, message
>>>

Виклики журналювання (logger.debug(), logger.info() тощо) приймають лише позиційні параметри для самого фактичного повідомлення журналу, а параметри ключових слів використовуються лише для визначення варіантів обробки фактичного виклик журналювання (наприклад, параметр ключового слова exc_info, щоб вказати, що слід реєструвати інформацію відстеження, або параметр ключового слова extra, щоб вказати додаткову контекстну інформацію, яку потрібно додати до журналу). Таким чином, ви не можете безпосередньо здійснювати виклики журналювання за допомогою синтаксису str.format() або string.Template, тому що внутрішньо пакет журналювання використовує %-formatting для об’єднання рядка формату та змінних аргументів. Це не змінюватиметься, зберігаючи зворотну сумісність, оскільки всі виклики журналювання, які присутні в існуючому коді, використовуватимуть рядки %-format.

Однак існує спосіб, за допомогою якого ви можете використовувати форматування {}- і $- для створення ваших індивідуальних повідомлень журналу. Згадайте, що для повідомлення ви можете використовувати довільний об’єкт як рядок формату повідомлення, і що пакет журналювання викличе str() для цього об’єкта, щоб отримати фактичний рядок формату. Розглянемо наступні два класи:

class BraceMessage:
    def __init__(self, fmt, /, *args, **kwargs):
        self.fmt = fmt
        self.args = args
        self.kwargs = kwargs

    def __str__(self):
        return self.fmt.format(*self.args, **self.kwargs)

class DollarMessage:
    def __init__(self, fmt, /, **kwargs):
        self.fmt = fmt
        self.kwargs = kwargs

    def __str__(self):
        from string import Template
        return Template(self.fmt).substitute(**self.kwargs)

Будь-який із них можна використовувати замість рядка формату, щоб дозволити використовувати {}- або $-форматування для побудови фактичної частини «повідомлення», яка з’являється у відформатованому журналі замість «%(message)s» або «{message». }» або «$message». Трохи громіздко використовувати назви класів, коли ви хочете щось зареєструвати, але це цілком приємно, якщо ви використовуєте псевдонім, наприклад __ (подвійне підкреслення — не плутати з _, єдине підкреслення, яке використовується як синонім/псевдонім для gettext.gettext() або його братів).

Наведені вище класи не включені в Python, хоча їх достатньо легко скопіювати та вставити у свій власний код. Їх можна використовувати наступним чином (за умови, що вони оголошені в модулі під назвою wherever):

>>> from wherever import BraceMessage as __
>>> print(__('Message with {0} {name}', 2, name='placeholders'))
Message with 2 placeholders
>>> class Point: pass
...
>>> p = Point()
>>> p.x = 0.5
>>> p.y = 0.5
>>> print(__('Message with coordinates: ({point.x:.2f}, {point.y:.2f})',
...       point=p))
Message with coordinates: (0.50, 0.50)
>>> from wherever import DollarMessage as __
>>> print(__('Message with $num $what', num=2, what='placeholders'))
Message with 2 placeholders
>>>

У той час як у наведених вище прикладах використовується print(), щоб показати, як працює форматування, ви, звичайно, скористаєтеся logger.debug() або подібним, щоб справді вести журнал за допомогою цього підходу.

One thing to note is that you pay no significant performance penalty with this approach: the actual formatting happens not when you make the logging call, but when (and if) the logged message is actually about to be output to a log by a handler. So the only slightly unusual thing which might trip you up is that the parentheses go around the format string and the arguments, not just the format string. That’s because the __ notation is just syntax sugar for a constructor call to one of the XXXMessage classes.

Якщо ви віддаєте перевагу, ви можете використовувати LoggerAdapter, щоб досягти ефекту, подібного до наведеного вище, як у наступному прикладі:

import logging

class Message:
    def __init__(self, fmt, args):
        self.fmt = fmt
        self.args = args

    def __str__(self):
        return self.fmt.format(*self.args)

class StyleAdapter(logging.LoggerAdapter):
    def log(self, level, msg, /, *args, stacklevel=1, **kwargs):
        if self.isEnabledFor(level):
            msg, kwargs = self.process(msg, kwargs)
            self.logger.log(level, Message(msg, args), **kwargs,
                            stacklevel=stacklevel+1)

logger = StyleAdapter(logging.getLogger(__name__))

def main():
    logger.debug('Hello, {}', 'world!')

if __name__ == '__main__':
    logging.basicConfig(level=logging.DEBUG)
    main()

The above script should log the message Hello, world! when run with Python 3.8 or later.

Налаштування LogRecord

Кожна подія журналювання представлена екземпляром LogRecord. Коли подія реєструється і не відфільтровується на рівні реєстратора, створюється LogRecord, заповнюється інформацією про подію, а потім передається обробникам цього реєстратора (і його предків, аж до реєстратора включно). де подальше поширення вгору по ієрархії вимкнено). До Python 3.2 існувало лише два місця, де це було зроблено:

  • Logger.makeRecord(), який викликається в звичайному процесі реєстрації події. Це безпосередньо викликало LogRecord для створення екземпляра.

  • makeLogRecord(), який викликається зі словником, що містить атрибути, які потрібно додати до LogRecord. Це зазвичай викликається, коли через мережу отримано відповідний словник (наприклад, у формі pickle через SocketHandler або у формі JSON через HTTPHandler).

Зазвичай це означало, що якщо вам потрібно зробити щось особливе з LogRecord, ви повинні зробити одну з наступних дій.

  • Створіть свій власний підклас Logger, який замінює Logger.makeRecord(), і встановіть його за допомогою setLoggerClass() перед створенням будь-яких реєстраторів, які вас цікавлять.

  • Додайте Filter до реєстратора або обробника, який виконує необхідні спеціальні маніпуляції, які вам потрібні, коли викликається його метод filter().

Перший підхід був би трохи громіздким у сценарії, коли (скажімо) кілька різних бібліотек хотіли б робити різні речі. Кожен намагатиметься встановити власний підклас Logger, і виграє той, хто зробить це останнім.

Другий підхід працює досить добре для багатьох випадків, але не дозволяє вам, наприклад, використовувати спеціалізований підклас LogRecord. Розробники бібліотек можуть встановити відповідний фільтр для своїх реєстраторів, але вони повинні пам’ятати про це щоразу, коли вводять новий реєстратор (що вони робили б, просто додаючи нові пакунки чи модулі та виконуючи:

logger = logging.getLogger(__name__)

на рівні модуля). Ймовірно, це занадто багато речей, про які варто думати. Розробники також можуть додати фільтр до NullHandler, прикріпленого до їхнього реєстратора верхнього рівня, але це не буде викликано, якщо розробник програми приєднає обробник до реєстратора бібліотеки нижчого рівня — тому виведіть з цього обробника не відображатиме намірів розробника бібліотеки.

У Python 3.2 і пізніших версіях створення LogRecord здійснюється через фабрику, яку ви можете вказати. Фабрика — це просто виклик, який можна встановити за допомогою setLogRecordFactory() і запитати за допомогою getLogRecordFactory(). Фабрика викликається з тим самим підписом, що й конструктор LogRecord, оскільки LogRecord є параметром за замовчуванням для фабрики.

Цей підхід дозволяє спеціальній фабрикі контролювати всі аспекти створення LogRecord. Наприклад, ви можете повернути підклас або просто додати деякі додаткові атрибути до створеного запису, використовуючи шаблон, подібний до цього:

old_factory = logging.getLogRecordFactory()

def record_factory(*args, **kwargs):
    record = old_factory(*args, **kwargs)
    record.custom_attribute = 0xdecafbad
    return record

logging.setLogRecordFactory(record_factory)

Цей шаблон дозволяє різним бібліотекам об’єднувати фабрики разом, і якщо вони не перезаписують атрибути одна одної або ненавмисно перезаписують атрибути, надані як стандартні, не повинно бути сюрпризів. Однак слід мати на увазі, що кожна ланка в ланцюжку додає накладні витрати на час виконання для всіх операцій журналювання, і цю техніку слід використовувати лише тоді, коли використання Filter не забезпечує бажаного результату.

Subclassing QueueHandler and QueueListener- a ZeroMQ example

Subclass QueueHandler

Ви можете використовувати підклас QueueHandler для надсилання повідомлень в інші типи черг, наприклад, сокет ZeroMQ „publish“. У наведеному нижче прикладі сокет створюється окремо та передається обробнику (як його «черга»):

import zmq   # using pyzmq, the Python binding for ZeroMQ
import json  # for serializing records portably

ctx = zmq.Context()
sock = zmq.Socket(ctx, zmq.PUB)  # or zmq.PUSH, or other suitable value
sock.bind('tcp://*:5556')        # or wherever

class ZeroMQSocketHandler(QueueHandler):
    def enqueue(self, record):
        self.queue.send_json(record.__dict__)


handler = ZeroMQSocketHandler(sock)

Звичайно, існують інші способи організації цього, наприклад, передача даних, необхідних обробнику для створення сокета:

class ZeroMQSocketHandler(QueueHandler):
    def __init__(self, uri, socktype=zmq.PUB, ctx=None):
        self.ctx = ctx or zmq.Context()
        socket = zmq.Socket(self.ctx, socktype)
        socket.bind(uri)
        super().__init__(socket)

    def enqueue(self, record):
        self.queue.send_json(record.__dict__)

    def close(self):
        self.queue.close()

Subclass QueueListener

Ви також можете створити підклас QueueListener, щоб отримувати повідомлення з інших типів черг, наприклад, сокет ZeroMQ „subscribe“. Ось приклад:

class ZeroMQSocketListener(QueueListener):
    def __init__(self, uri, /, *handlers, **kwargs):
        self.ctx = kwargs.get('ctx') or zmq.Context()
        socket = zmq.Socket(self.ctx, zmq.SUB)
        socket.setsockopt_string(zmq.SUBSCRIBE, '')  # subscribe to everything
        socket.connect(uri)
        super().__init__(socket, *handlers, **kwargs)

    def dequeue(self):
        msg = self.queue.recv_json()
        return logging.makeLogRecord(msg)

Subclassing QueueHandler and QueueListener- a pynng example

In a similar way to the above section, we can implement a listener and handler using pynng, which is a Python binding to NNG, billed as a spiritual successor to ZeroMQ. The following snippets illustrate – you can test them in an environment which has pynng installed. Just for variety, we present the listener first.

Subclass QueueListener

# listener.py
import json
import logging
import logging.handlers

import pynng

DEFAULT_ADDR = "tcp://localhost:13232"

interrupted = False

class NNGSocketListener(logging.handlers.QueueListener):

    def __init__(self, uri, /, *handlers, **kwargs):
        # Have a timeout for interruptability, and open a
        # subscriber socket
        socket = pynng.Sub0(listen=uri, recv_timeout=500)
        # The b'' subscription matches all topics
        topics = kwargs.pop('topics', None) or b''
        socket.subscribe(topics)
        # We treat the socket as a queue
        super().__init__(socket, *handlers, **kwargs)

    def dequeue(self, block):
        data = None
        # Keep looping while not interrupted and no data received over the
        # socket
        while not interrupted:
            try:
                data = self.queue.recv(block=block)
                break
            except pynng.Timeout:
                pass
            except pynng.Closed:  # sometimes happens when you hit Ctrl-C
                break
        if data is None:
            return None
        # Get the logging event sent from a publisher
        event = json.loads(data.decode('utf-8'))
        return logging.makeLogRecord(event)

    def enqueue_sentinel(self):
        # Not used in this implementation, as the socket isn't really a
        # queue
        pass

logging.getLogger('pynng').propagate = False
listener = NNGSocketListener(DEFAULT_ADDR, logging.StreamHandler(), topics=b'')
listener.start()
print('Press Ctrl-C to stop.')
try:
    while True:
        pass
except KeyboardInterrupt:
    interrupted = True
finally:
    listener.stop()

Subclass QueueHandler

# sender.py
import json
import logging
import logging.handlers
import time
import random

import pynng

DEFAULT_ADDR = "tcp://localhost:13232"

class NNGSocketHandler(logging.handlers.QueueHandler):

    def __init__(self, uri):
        socket = pynng.Pub0(dial=uri, send_timeout=500)
        super().__init__(socket)

    def enqueue(self, record):
        # Send the record as UTF-8 encoded JSON
        d = dict(record.__dict__)
        data = json.dumps(d)
        self.queue.send(data.encode('utf-8'))

    def close(self):
        self.queue.close()

logging.getLogger('pynng').propagate = False
handler = NNGSocketHandler(DEFAULT_ADDR)
# Make sure the process ID is in the output
logging.basicConfig(level=logging.DEBUG,
                    handlers=[logging.StreamHandler(), handler],
                    format='%(levelname)-8s %(name)10s %(process)6s %(message)s')
levels = (logging.DEBUG, logging.INFO, logging.WARNING, logging.ERROR,
          logging.CRITICAL)
logger_names = ('myapp', 'myapp.lib1', 'myapp.lib2')
msgno = 1
while True:
    # Just randomly select some loggers and levels and log away
    level = random.choice(levels)
    logger = logging.getLogger(random.choice(logger_names))
    logger.log(level, 'Message no. %5d' % msgno)
    msgno += 1
    delay = random.random() * 2 + 0.5
    time.sleep(delay)

You can run the above two snippets in separate command shells. If we run the listener in one shell and run the sender in two separate shells, we should see something like the following. In the first sender shell:

$ python sender.py
DEBUG         myapp    613 Message no.     1
WARNING  myapp.lib2    613 Message no.     2
CRITICAL myapp.lib2    613 Message no.     3
WARNING  myapp.lib2    613 Message no.     4
CRITICAL myapp.lib1    613 Message no.     5
DEBUG         myapp    613 Message no.     6
CRITICAL myapp.lib1    613 Message no.     7
INFO     myapp.lib1    613 Message no.     8
(and so on)

In the second sender shell:

$ python sender.py
INFO     myapp.lib2    657 Message no.     1
CRITICAL myapp.lib2    657 Message no.     2
CRITICAL      myapp    657 Message no.     3
CRITICAL myapp.lib1    657 Message no.     4
INFO     myapp.lib1    657 Message no.     5
WARNING  myapp.lib2    657 Message no.     6
CRITICAL      myapp    657 Message no.     7
DEBUG    myapp.lib1    657 Message no.     8
(and so on)

In the listener shell:

$ python listener.py
Press Ctrl-C to stop.
DEBUG         myapp    613 Message no.     1
WARNING  myapp.lib2    613 Message no.     2
INFO     myapp.lib2    657 Message no.     1
CRITICAL myapp.lib2    613 Message no.     3
CRITICAL myapp.lib2    657 Message no.     2
CRITICAL      myapp    657 Message no.     3
WARNING  myapp.lib2    613 Message no.     4
CRITICAL myapp.lib1    613 Message no.     5
CRITICAL myapp.lib1    657 Message no.     4
INFO     myapp.lib1    657 Message no.     5
DEBUG         myapp    613 Message no.     6
WARNING  myapp.lib2    657 Message no.     6
CRITICAL      myapp    657 Message no.     7
CRITICAL myapp.lib1    613 Message no.     7
INFO     myapp.lib1    613 Message no.     8
DEBUG    myapp.lib1    657 Message no.     8
(and so on)

As you can see, the logging from the two sender processes is interleaved in the listener’s output.

Приклад конфігурації на основі словника

Нижче наведено приклад словника конфігурації журналювання — його взято з документації проекту Django. Цей словник передається до dictConfig() для введення в дію конфігурації:

LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'formatters': {
        'verbose': {
            'format': '{levelname} {asctime} {module} {process:d} {thread:d} {message}',
            'style': '{',
        },
        'simple': {
            'format': '{levelname} {message}',
            'style': '{',
        },
    },
    'filters': {
        'special': {
            '()': 'project.logging.SpecialFilter',
            'foo': 'bar',
        },
    },
    'handlers': {
        'console': {
            'level': 'INFO',
            'class': 'logging.StreamHandler',
            'formatter': 'simple',
        },
        'mail_admins': {
            'level': 'ERROR',
            'class': 'django.utils.log.AdminEmailHandler',
            'filters': ['special']
        }
    },
    'loggers': {
        'django': {
            'handlers': ['console'],
            'propagate': True,
        },
        'django.request': {
            'handlers': ['mail_admins'],
            'level': 'ERROR',
            'propagate': False,
        },
        'myproject.custom': {
            'handlers': ['console', 'mail_admins'],
            'level': 'INFO',
            'filters': ['special']
        }
    }
}

Для отримання додаткової інформації про цю конфігурацію ви можете переглянути відповідний розділ документації Django.

Використання ротатора та іменника для налаштування обробки ротації журналу

An example of how you can define a namer and rotator is given in the following runnable script, which shows gzip compression of the log file:

import gzip
import logging
import logging.handlers
import os
import shutil

def namer(name):
    return name + ".gz"

def rotator(source, dest):
    with open(source, 'rb') as f_in:
        with gzip.open(dest, 'wb') as f_out:
            shutil.copyfileobj(f_in, f_out)
    os.remove(source)


rh = logging.handlers.RotatingFileHandler('rotated.log', maxBytes=128, backupCount=5)
rh.rotator = rotator
rh.namer = namer

root = logging.getLogger()
root.setLevel(logging.INFO)
root.addHandler(rh)
f = logging.Formatter('%(asctime)s %(message)s')
rh.setFormatter(f)
for i in range(1000):
    root.info(f'Message no. {i + 1}')

After running this, you will see six new files, five of which are compressed:

$ ls rotated.log*
rotated.log       rotated.log.2.gz  rotated.log.4.gz
rotated.log.1.gz  rotated.log.3.gz  rotated.log.5.gz
$ zcat rotated.log.1.gz
2023-01-20 02:28:17,767 Message no. 996
2023-01-20 02:28:17,767 Message no. 997
2023-01-20 02:28:17,767 Message no. 998

Більш складний приклад багатопроцесорної обробки

Наступний робочий приклад показує, як журналювання можна використовувати з багатопроцесорною обробкою за допомогою файлів конфігурації. Конфігурації досить прості, але служать для ілюстрації того, як більш складні можуть бути реалізовані в реальному багатопроцесорному сценарії.

У прикладі головний процес породжує процес слухача та деякі робочі процеси. Кожен із основного процесу, слухача та робочих має три окремі конфігурації (усі робочі мають однакову конфігурацію). Ми можемо побачити реєстрацію в основному процесі, як працівники входять до QueueHandler і як слухач реалізує QueueListener і більш складну конфігурацію журналювання, а також організовує відправку подій, отриманих через чергу, до обробників, указаних у конфігурації. Зауважте, що ці конфігурації є суто ілюстративними, але ви зможете адаптувати цей приклад до власного сценарію.

Ось сценарій - рядки документації та коментарі, сподіваюся, пояснюють, як це працює:

import logging
import logging.config
import logging.handlers
from multiprocessing import Process, Queue, Event, current_process
import os
import random
import time

class MyHandler:
    """
    A simple handler for logging events. It runs in the listener process and
    dispatches events to loggers based on the name in the received record,
    which then get dispatched, by the logging system, to the handlers
    configured for those loggers.
    """

    def handle(self, record):
        if record.name == "root":
            logger = logging.getLogger()
        else:
            logger = logging.getLogger(record.name)

        if logger.isEnabledFor(record.levelno):
            # The process name is transformed just to show that it's the listener
            # doing the logging to files and console
            record.processName = '%s (for %s)' % (current_process().name, record.processName)
            logger.handle(record)

def listener_process(q, stop_event, config):
    """
    This could be done in the main process, but is just done in a separate
    process for illustrative purposes.

    This initialises logging according to the specified configuration,
    starts the listener and waits for the main process to signal completion
    via the event. The listener is then stopped, and the process exits.
    """
    logging.config.dictConfig(config)
    listener = logging.handlers.QueueListener(q, MyHandler())
    listener.start()
    if os.name == 'posix':
        # On POSIX, the setup logger will have been configured in the
        # parent process, but should have been disabled following the
        # dictConfig call.
        # On Windows, since fork isn't used, the setup logger won't
        # exist in the child, so it would be created and the message
        # would appear - hence the "if posix" clause.
        logger = logging.getLogger('setup')
        logger.critical('Should not appear, because of disabled logger ...')
    stop_event.wait()
    listener.stop()

def worker_process(config):
    """
    A number of these are spawned for the purpose of illustration. In
    practice, they could be a heterogeneous bunch of processes rather than
    ones which are identical to each other.

    This initialises logging according to the specified configuration,
    and logs a hundred messages with random levels to randomly selected
    loggers.

    A small sleep is added to allow other processes a chance to run. This
    is not strictly needed, but it mixes the output from the different
    processes a bit more than if it's left out.
    """
    logging.config.dictConfig(config)
    levels = [logging.DEBUG, logging.INFO, logging.WARNING, logging.ERROR,
              logging.CRITICAL]
    loggers = ['foo', 'foo.bar', 'foo.bar.baz',
               'spam', 'spam.ham', 'spam.ham.eggs']
    if os.name == 'posix':
        # On POSIX, the setup logger will have been configured in the
        # parent process, but should have been disabled following the
        # dictConfig call.
        # On Windows, since fork isn't used, the setup logger won't
        # exist in the child, so it would be created and the message
        # would appear - hence the "if posix" clause.
        logger = logging.getLogger('setup')
        logger.critical('Should not appear, because of disabled logger ...')
    for i in range(100):
        lvl = random.choice(levels)
        logger = logging.getLogger(random.choice(loggers))
        logger.log(lvl, 'Message no. %d', i)
        time.sleep(0.01)

def main():
    q = Queue()
    # The main process gets a simple configuration which prints to the console.
    config_initial = {
        'version': 1,
        'handlers': {
            'console': {
                'class': 'logging.StreamHandler',
                'level': 'INFO'
            }
        },
        'root': {
            'handlers': ['console'],
            'level': 'DEBUG'
        }
    }
    # The worker process configuration is just a QueueHandler attached to the
    # root logger, which allows all messages to be sent to the queue.
    # We disable existing loggers to disable the "setup" logger used in the
    # parent process. This is needed on POSIX because the logger will
    # be there in the child following a fork().
    config_worker = {
        'version': 1,
        'disable_existing_loggers': True,
        'handlers': {
            'queue': {
                'class': 'logging.handlers.QueueHandler',
                'queue': q
            }
        },
        'root': {
            'handlers': ['queue'],
            'level': 'DEBUG'
        }
    }
    # The listener process configuration shows that the full flexibility of
    # logging configuration is available to dispatch events to handlers however
    # you want.
    # We disable existing loggers to disable the "setup" logger used in the
    # parent process. This is needed on POSIX because the logger will
    # be there in the child following a fork().
    config_listener = {
        'version': 1,
        'disable_existing_loggers': True,
        'formatters': {
            'detailed': {
                'class': 'logging.Formatter',
                'format': '%(asctime)s %(name)-15s %(levelname)-8s %(processName)-10s %(message)s'
            },
            'simple': {
                'class': 'logging.Formatter',
                'format': '%(name)-15s %(levelname)-8s %(processName)-10s %(message)s'
            }
        },
        'handlers': {
            'console': {
                'class': 'logging.StreamHandler',
                'formatter': 'simple',
                'level': 'INFO'
            },
            'file': {
                'class': 'logging.FileHandler',
                'filename': 'mplog.log',
                'mode': 'w',
                'formatter': 'detailed'
            },
            'foofile': {
                'class': 'logging.FileHandler',
                'filename': 'mplog-foo.log',
                'mode': 'w',
                'formatter': 'detailed'
            },
            'errors': {
                'class': 'logging.FileHandler',
                'filename': 'mplog-errors.log',
                'mode': 'w',
                'formatter': 'detailed',
                'level': 'ERROR'
            }
        },
        'loggers': {
            'foo': {
                'handlers': ['foofile']
            }
        },
        'root': {
            'handlers': ['console', 'file', 'errors'],
            'level': 'DEBUG'
        }
    }
    # Log some initial events, just to show that logging in the parent works
    # normally.
    logging.config.dictConfig(config_initial)
    logger = logging.getLogger('setup')
    logger.info('About to create workers ...')
    workers = []
    for i in range(5):
        wp = Process(target=worker_process, name='worker %d' % (i + 1),
                     args=(config_worker,))
        workers.append(wp)
        wp.start()
        logger.info('Started worker: %s', wp.name)
    logger.info('About to create listener ...')
    stop_event = Event()
    lp = Process(target=listener_process, name='listener',
                 args=(q, stop_event, config_listener))
    lp.start()
    logger.info('Started listener')
    # We now hang around for the workers to finish their work.
    for wp in workers:
        wp.join()
    # Workers all done, listening can now stop.
    # Logging in the parent still works normally.
    logger.info('Telling listener to stop ...')
    stop_event.set()
    lp.join()
    logger.info('All done.')

if __name__ == '__main__':
    main()

Вставлення BOM у повідомлення, надіслані до SysLogHandler

RFC 5424 вимагає, щоб повідомлення Unicode надсилалося до демона системного журналу як набір байтів із такою структурою: необов’язковий чистий ASCII-компонент, за яким слідує UTF-8 Byte Order Mark (BOM), за яким слідує Юнікод, закодований за допомогою UTF-8. (Див. відповідний розділ специфікації.)

У Python 3.1 до SysLogHandler було додано код для вставлення BOM у повідомлення, але, на жаль, він був реалізований неправильно, оскільки BOM з’являвся на початку повідомлення, а отже, не дозволяв будь-які компонент pure-ASCII, який з’явиться перед ним.

Оскільки ця поведінка порушена, неправильний код вставки BOM видаляється з Python 3.2.4 і пізніших версій. Однак його не буде замінено, і якщо ви хочете створювати повідомлення, сумісні з RFC 5424, які включають специфікацію матеріалів, необов’язкову чисту послідовність ASCII перед нею та довільний код Unicode після неї, закодований за допомогою UTF-8, тоді ви потрібно зробити наступне:

  1. Приєднайте екземпляр Formatter до вашого екземпляра SysLogHandler із рядком форматування, наприклад:

    'ASCII section\ufeffUnicode section'
    

    Кодова точка Юнікоду U+FEFF, коли вона кодується за допомогою UTF-8, буде закодована як UTF-8 BOM – рядок байтів b'\xef\xbb\xbf.

  2. Замініть розділ ASCII будь-якими заповнювачами, але переконайтеся, що дані, які з’являються в ньому після заміни, завжди мають ASCII (таким чином вони залишаться незмінними після кодування UTF-8).

  3. Замініть розділ Unicode будь-якими заповнювачами; якщо дані, які з’являються там після заміни, містять символи поза діапазоном ASCII, це нормально – вони будуть закодовані за допомогою UTF-8.

Відформатоване повідомлення буде закодовано за допомогою кодування UTF-8 за допомогою SysLogHandler. Якщо ви дотримуєтеся наведених вище правил, ви зможете створювати RFC 5424-сумісні повідомлення. Якщо ви цього не зробите, журналювання може не скаржитися, але ваші повідомлення не будуть сумісними з RFC 5424, і ваш демон системного журналу може скаржитися.

Впровадження структурованого журналювання

Незважаючи на те, що більшість повідомлень журналу призначені для читання людьми, і тому їх не легко аналізувати машиною, можуть виникнути обставини, коли ви захочете вивести повідомлення у структурованому форматі, який може проаналізувати програма (без потреби у складних регулярних виразах). щоб проаналізувати повідомлення журналу). Це легко досягти за допомогою пакета журналювання. Існує кілька способів, за допомогою яких цього можна досягти, але нижче наведено простий підхід, який використовує JSON для серіалізації події машинним способом:

import json
import logging

class StructuredMessage:
    def __init__(self, message, /, **kwargs):
        self.message = message
        self.kwargs = kwargs

    def __str__(self):
        return '%s >>> %s' % (self.message, json.dumps(self.kwargs))

_ = StructuredMessage   # optional, to improve readability

logging.basicConfig(level=logging.INFO, format='%(message)s')
logging.info(_('message 1', foo='bar', bar='baz', num=123, fnum=123.456))

Якщо наведений вище сценарій запущено, він друкує:

message 1 >>> {"fnum": 123.456, "num": 123, "bar": "baz", "foo": "bar"}

Зауважте, що порядок елементів може відрізнятися залежно від версії Python, що використовується.

Якщо вам потрібна більш спеціалізована обробка, ви можете використовувати спеціальний кодер JSON, як у наступному повному прикладі:

import json
import logging


class Encoder(json.JSONEncoder):
    def default(self, o):
        if isinstance(o, set):
            return tuple(o)
        elif isinstance(o, str):
            return o.encode('unicode_escape').decode('ascii')
        return super().default(o)

class StructuredMessage:
    def __init__(self, message, /, **kwargs):
        self.message = message
        self.kwargs = kwargs

    def __str__(self):
        s = Encoder().encode(self.kwargs)
        return '%s >>> %s' % (self.message, s)

_ = StructuredMessage   # optional, to improve readability

def main():
    logging.basicConfig(level=logging.INFO, format='%(message)s')
    logging.info(_('message 1', set_value={1, 2, 3}, snowman='\u2603'))

if __name__ == '__main__':
    main()

Коли наведений вище сценарій виконується, він друкує:

message 1 >>> {"snowman": "\u2603", "set_value": [1, 2, 3]}

Зауважте, що порядок елементів може відрізнятися залежно від версії Python, що використовується.

Налаштування обробників за допомогою dictConfig()

Бувають випадки, коли ви хочете налаштувати обробники журналу певним чином, і якщо ви використовуєте dictConfig(), ви можете зробити це без підкласів. Як приклад, подумайте, що ви можете встановити право власності на файл журналу. У POSIX це легко зробити за допомогою shutil.chown(), але обробники файлів у stdlib не пропонують вбудованої підтримки. Ви можете налаштувати створення обробника за допомогою простої функції, такої як:

def owned_file_handler(filename, mode='a', encoding=None, owner=None):
    if owner:
        if not os.path.exists(filename):
            open(filename, 'a').close()
        shutil.chown(filename, *owner)
    return logging.FileHandler(filename, mode, encoding)

Потім ви можете вказати в конфігурації журналювання, переданій у dictConfig(), щоб обробник журналювання було створено шляхом виклику цієї функції:

LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'formatters': {
        'default': {
            'format': '%(asctime)s %(levelname)s %(name)s %(message)s'
        },
    },
    'handlers': {
        'file':{
            # The values below are popped from this dictionary and
            # used to create the handler, set the handler's level and
            # its formatter.
            '()': owned_file_handler,
            'level':'DEBUG',
            'formatter': 'default',
            # The values below are passed to the handler creator callable
            # as keyword arguments.
            'owner': ['pulse', 'pulse'],
            'filename': 'chowntest.log',
            'mode': 'w',
            'encoding': 'utf-8',
        },
    },
    'root': {
        'handlers': ['file'],
        'level': 'DEBUG',
    },
}

У цьому прикладі я встановлюю право власності за допомогою користувача та групи pulse лише для ілюстрації. Об’єднавши це в робочий сценарій, chowntest.py:

import logging, logging.config, os, shutil

def owned_file_handler(filename, mode='a', encoding=None, owner=None):
    if owner:
        if not os.path.exists(filename):
            open(filename, 'a').close()
        shutil.chown(filename, *owner)
    return logging.FileHandler(filename, mode, encoding)

LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'formatters': {
        'default': {
            'format': '%(asctime)s %(levelname)s %(name)s %(message)s'
        },
    },
    'handlers': {
        'file':{
            # The values below are popped from this dictionary and
            # used to create the handler, set the handler's level and
            # its formatter.
            '()': owned_file_handler,
            'level':'DEBUG',
            'formatter': 'default',
            # The values below are passed to the handler creator callable
            # as keyword arguments.
            'owner': ['pulse', 'pulse'],
            'filename': 'chowntest.log',
            'mode': 'w',
            'encoding': 'utf-8',
        },
    },
    'root': {
        'handlers': ['file'],
        'level': 'DEBUG',
    },
}

logging.config.dictConfig(LOGGING)
logger = logging.getLogger('mylogger')
logger.debug('A debug message')

Щоб запустити це, вам, ймовірно, потрібно буде запустити як root:

$ sudo python3.3 chowntest.py
$ cat chowntest.log
2013-11-05 09:34:51,128 DEBUG mylogger A debug message
$ ls -l chowntest.log
-rw-r--r-- 1 pulse pulse 55 2013-11-05 09:34 chowntest.log

Зверніть увагу, що в цьому прикладі використовується Python 3.3, тому що саме там з’являється shutil.chown(). Цей підхід має працювати з будь-якою версією Python, яка підтримує dictConfig(), а саме з Python 2.7, 3.2 або новішою. У версіях до 3.3 вам потрібно було б реалізувати фактичну зміну власності, використовуючи, наприклад, os.chown().

На практиці функція створення обробника може бути десь у службовому модулі вашого проекту. Замість рядка в конфігурації:

'()': owned_file_handler,

ви можете використовувати, наприклад:

'()': 'ext://project.util.owned_file_handler',

де project.util можна замінити фактичною назвою пакета, де знаходиться функція. У наведеному вище робочому сценарії використання 'ext://__main__.owned_file_handler має працювати. Тут фактичний виклик визначається за допомогою dictConfig() із специфікації ext://.

Сподіваємось, цей приклад також вказує шлях до того, як ви можете реалізувати інші типи змін файлів - наприклад. встановлення певних бітів дозволу POSIX - таким же чином, за допомогою os.chmod().

Звичайно, цей підхід також можна розширити до типів обробників, відмінних від FileHandler - наприклад, одного з обробників файлів, що обертаються, або зовсім іншого типу обробника.

Використання певних стилів форматування у вашій програмі

У Python 3.2 Formatter отримав параметр ключового слова style, який, незважаючи на значення за умовчанням % для зворотної сумісності, дозволяв специфікацію { або $ для підтримки підходів до форматування, які підтримуються str.format() і string.Template. Зауважте, що це керує форматуванням повідомлень журналу для остаточного виведення в журнали та повністю ортогонально до того, як будується окреме повідомлення журналу.

Logging calls (debug(), info() etc.) only take positional parameters for the actual logging message itself, with keyword parameters used only for determining options for how to handle the logging call (e.g. the exc_info keyword parameter to indicate that traceback information should be logged, or the extra keyword parameter to indicate additional contextual information to be added to the log). So you cannot directly make logging calls using str.format() or string.Template syntax, because internally the logging package uses %-formatting to merge the format string and the variable arguments. There would be no changing this while preserving backward compatibility, since all logging calls which are out there in existing code will be using %-format strings.

Були пропозиції пов’язати стилі формату з певними реєстраторами, але такий підхід також стикається з проблемами зворотної сумісності, оскільки будь-який існуючий код може використовувати дане ім’я реєстратора та використовувати %-formatting.

Щоб журналювання працювало сумісно між будь-якими сторонніми бібліотеками та вашим кодом, рішення щодо форматування потрібно приймати на рівні окремого виклику журналювання. Це відкриває кілька способів використання альтернативних стилів форматування.

Використання фабрик LogRecord

У Python 3.2 разом зі змінами Formatter, згаданими вище, пакет журналювання отримав можливість дозволяти користувачам встановлювати власні підкласи LogRecord за допомогою функції setLogRecordFactory() . Ви можете використовувати це, щоб установити власний підклас LogRecord, який робить правильні речі, замінюючи метод getMessage(). Реалізація базового класу цього методу є місцем, де відбувається форматування msg % args, і де ви можете замінити своє альтернативне форматування; однак ви повинні бути обережними, щоб підтримувати всі стилі форматування та дозволити %-formatting як типовий, щоб забезпечити взаємодію з іншим кодом. Слід також подбати про те, щоб викликати str(self.msg), як це робить базова реалізація.

Зверніться до довідкової документації щодо setLogRecordFactory() і LogRecord для отримання додаткової інформації.

Використання настроюваних об’єктів повідомлення

Існує інший, можливо, простіший спосіб використання форматування {}- і $- для створення індивідуальних повідомлень журналу. Ви можете пам’ятати (з Використання довільних об’єктів як повідомлень), що під час журналювання ви можете використовувати довільний об’єкт як рядок формату повідомлення, і що пакет журналювання викличе str() для цього об’єкта, щоб отримати рядок фактичного формату. Розглянемо наступні два класи:

class BraceMessage:
    def __init__(self, fmt, /, *args, **kwargs):
        self.fmt = fmt
        self.args = args
        self.kwargs = kwargs

    def __str__(self):
        return self.fmt.format(*self.args, **self.kwargs)

class DollarMessage:
    def __init__(self, fmt, /, **kwargs):
        self.fmt = fmt
        self.kwargs = kwargs

    def __str__(self):
        from string import Template
        return Template(self.fmt).substitute(**self.kwargs)

Будь-яке з них можна використовувати замість рядка формату, щоб дозволити використовувати {}- або $-форматування для створення фактичної частини «повідомлення», яка з’являється у відформатованому журналі замість «%(message)s» або «{message». }» або «$message». Якщо вам здається трохи громіздким використовувати імена класів, коли ви хочете щось зареєструвати, ви можете зробити це більш приємним, якщо використаєте псевдонім, наприклад M або _ для повідомлення (або, можливо __, якщо ви використовуєте _ для локалізації).

Приклади цього підходу наведені нижче. По-перше, форматування за допомогою str.format():

>>> __ = BraceMessage
>>> print(__('Message with {0} {1}', 2, 'placeholders'))
Message with 2 placeholders
>>> class Point: pass
...
>>> p = Point()
>>> p.x = 0.5
>>> p.y = 0.5
>>> print(__('Message with coordinates: ({point.x:.2f}, {point.y:.2f})', point=p))
Message with coordinates: (0.50, 0.50)

По-друге, форматування за допомогою string.Template:

>>> __ = DollarMessage
>>> print(__('Message with $num $what', num=2, what='placeholders'))
Message with 2 placeholders
>>>

One thing to note is that you pay no significant performance penalty with this approach: the actual formatting happens not when you make the logging call, but when (and if) the logged message is actually about to be output to a log by a handler. So the only slightly unusual thing which might trip you up is that the parentheses go around the format string and the arguments, not just the format string. That’s because the __ notation is just syntax sugar for a constructor call to one of the XXXMessage classes shown above.

Налаштування фільтрів за допомогою dictConfig()

Ви можете налаштувати фільтри за допомогою dictConfig(), хоча на перший погляд може бути неочевидно, як це зробити (тому цей рецепт). Оскільки Filter є єдиним класом фільтра, включеним у стандартну бібліотеку, і він навряд чи задовольнить багато вимог (він існує лише як базовий клас), вам зазвичай потрібно буде визначити свій власний Підклас Filter із перевизначеним методом filter(). Для цього вкажіть ключ () у словнику конфігурації для фільтра, вказавши виклик, який буде використовуватися для створення фільтра (клас є найбільш очевидним, але ви можете надати будь-який виклик, який повертає Filter екземпляр). Ось повний приклад:

import logging
import logging.config
import sys

class MyFilter(logging.Filter):
    def __init__(self, param=None):
        self.param = param

    def filter(self, record):
        if self.param is None:
            allow = True
        else:
            allow = self.param not in record.msg
        if allow:
            record.msg = 'changed: ' + record.msg
        return allow

LOGGING = {
    'version': 1,
    'filters': {
        'myfilter': {
            '()': MyFilter,
            'param': 'noshow',
        }
    },
    'handlers': {
        'console': {
            'class': 'logging.StreamHandler',
            'filters': ['myfilter']
        }
    },
    'root': {
        'level': 'DEBUG',
        'handlers': ['console']
    },
}

if __name__ == '__main__':
    logging.config.dictConfig(LOGGING)
    logging.debug('hello')
    logging.debug('hello - noshow')

У цьому прикладі показано, як ви можете передати дані конфігурації викликаному, який створює екземпляр, у формі параметрів ключових слів. Під час запуску наведений вище сценарій надрукує:

changed: hello

який показує, що фільтр працює, як налаштовано.

Кілька додаткових моментів, на які варто звернути увагу:

  • Якщо ви не можете звернутися до викликаного безпосередньо в конфігурації (наприклад, якщо він живе в іншому модулі, і ви не можете імпортувати його безпосередньо туди, де знаходиться словник конфігурації), ви можете використовувати форму ext://. .., як описано в Access to external objects. Наприклад, у наведеному вище прикладі ви могли використати текст 'ext://__main__.MyFilter замість MyFilter.

  • Як і для фільтрів, цю техніку також можна використовувати для налаштування нестандартних обробників і форматувальників. Перегляньте User-defined objects, щоб дізнатися більше про те, як ведення журналу підтримує використання визначених користувачем об’єктів у своїй конфігурації, і перегляньте інший рецепт кулінарної книги Налаштування обробників за допомогою dictConfig() вище.

Індивідуальне форматування винятків

Можуть бути випадки, коли ви захочете зробити налаштоване форматування винятків - заради аргументу, припустімо, що вам потрібен рівно один рядок на зареєстровану подію, навіть якщо присутня інформація про винятки. Ви можете зробити це за допомогою спеціального класу форматера, як показано в наступному прикладі:

import logging

class OneLineExceptionFormatter(logging.Formatter):
    def formatException(self, exc_info):
        """
        Format an exception so that it prints on a single line.
        """
        result = super().formatException(exc_info)
        return repr(result)  # or format into one line however you want to

    def format(self, record):
        s = super().format(record)
        if record.exc_text:
            s = s.replace('\n', '') + '|'
        return s

def configure_logging():
    fh = logging.FileHandler('output.txt', 'w')
    f = OneLineExceptionFormatter('%(asctime)s|%(levelname)s|%(message)s|',
                                  '%d/%m/%Y %H:%M:%S')
    fh.setFormatter(f)
    root = logging.getLogger()
    root.setLevel(logging.DEBUG)
    root.addHandler(fh)

def main():
    configure_logging()
    logging.info('Sample message')
    try:
        x = 1 / 0
    except ZeroDivisionError as e:
        logging.exception('ZeroDivisionError: %s', e)

if __name__ == '__main__':
    main()

Під час запуску створюється файл із рівно двома рядками:

28/01/2015 07:21:23|INFO|Sample message|
28/01/2015 07:21:23|ERROR|ZeroDivisionError: integer division or modulo by zero|'Traceback (most recent call last):\n  File "logtest7.py", line 30, in main\n    x = 1 / 0\nZeroDivisionError: integer division or modulo by zero'|

Хоча описане вище лікування є спрощеним, воно вказує шлях до того, як інформацію про винятки можна відформатувати на свій смак. Модуль traceback може бути корисним для більш спеціалізованих потреб.

Озвучення повідомлень журналу

Можуть бути ситуації, коли бажано, щоб повідомлення журналу відтворювалися в звуковому, а не у видимому форматі. Це легко зробити, якщо у вашій системі доступна функція перетворення тексту в мову (TTS), навіть якщо вона не має прив’язки Python. Більшість систем TTS мають програму командного рядка, яку можна запустити, і її можна викликати з обробника за допомогою subprocess. Тут передбачається, що програми командного рядка TTS не будуть взаємодіяти з користувачами або займати багато часу для виконання, і що частота зареєстрованих повідомлень не буде настільки високою, щоб завалювати користувача повідомленнями, і що прийнятно мати повідомлення промовляються по одному, а не одночасно. Приклад реалізації нижче очікує, поки одне повідомлення буде озвучено перед обробкою наступного, і це може спричинити очікування інших обробників. Ось короткий приклад, який демонструє підхід, який передбачає наявність пакета TTS espeak:

import logging
import subprocess
import sys

class TTSHandler(logging.Handler):
    def emit(self, record):
        msg = self.format(record)
        # Speak slowly in a female English voice
        cmd = ['espeak', '-s150', '-ven+f3', msg]
        p = subprocess.Popen(cmd, stdout=subprocess.PIPE,
                             stderr=subprocess.STDOUT)
        # wait for the program to finish
        p.communicate()

def configure_logging():
    h = TTSHandler()
    root = logging.getLogger()
    root.addHandler(h)
    # the default formatter just returns the message
    root.setLevel(logging.DEBUG)

def main():
    logging.info('Hello')
    logging.debug('Goodbye')

if __name__ == '__main__':
    configure_logging()
    sys.exit(main())

Під час запуску цей скрипт має сказати «Привіт», а потім «До побачення» жіночим голосом.

Наведений вище підхід, звичайно, можна адаптувати до інших систем TTS і навіть до інших систем взагалі, які можуть обробляти повідомлення через зовнішні програми, що запускаються з командного рядка.

Буферизація повідомлень журналу та їх умовне виведення

Можуть виникнути ситуації, коли потрібно реєструвати повідомлення у тимчасовій області та виводити їх лише у разі виникнення певної умови. Наприклад, ви можете почати реєструвати події налагодження у функції, і якщо функція завершиться без помилок, ви не хочете захаращувати журнал зібраною інформацією про налагодження, але якщо є помилка, ви хочете, щоб усі налагодження інформацію, яку потрібно вивести, а також помилку.

Ось приклад, який показує, як ви можете зробити це за допомогою декоратора для ваших функцій, де ви хочете, щоб журналювання поводилося таким чином. Він використовує logging.handlers.MemoryHandler, який дозволяє буферизувати зареєстровані події, доки не відбудеться певна умова, після чого буферизовані події скидаються і передаються іншому обробнику (ціль обробник) для обробки. За замовчуванням MemoryHandler очищується, коли його буфер заповнюється або спостерігається подія, рівень якої перевищує або дорівнює вказаному порогу. Ви можете використовувати цей рецепт із більш спеціалізованим підкласом MemoryHandler, якщо вам потрібна спеціальна поведінка очищення.

Приклад сценарію має просту функцію, foo, яка просто циклічно перебирає всі рівні журналювання, записуючи в sys.stderr, щоб сказати, на якому рівні він збирається зареєструватися, а потім фактично записує повідомлення на цьому рівень. Ви можете передати параметр у foo, який, якщо істина, буде реєструватися на рівнях ПОМИЛКА та КРИТИЧНИЙ - інакше він реєструватиме лише на рівнях НАЛАШТУВАННЯ, ІНФОРМАЦІЇ та ПОПЕРЕДЖЕННЯ.

Сценарій лише організовує декорування foo за допомогою декоратора, який виконуватиме необхідне умовне журналювання. Декоратор приймає реєстратор як параметр і приєднує обробник пам’яті на час виклику декорованої функції. Декоратор може бути додатково параметризований за допомогою цільового обробника, рівня, на якому має відбуватися очищення, і ємності для буфера (кількість записів, буферизованих). За умовчанням це StreamHandler, який записує в sys.stderr, logging.ERROR і 100 відповідно.

Ось сценарій:

import logging
from logging.handlers import MemoryHandler
import sys

logger = logging.getLogger(__name__)
logger.addHandler(logging.NullHandler())

def log_if_errors(logger, target_handler=None, flush_level=None, capacity=None):
    if target_handler is None:
        target_handler = logging.StreamHandler()
    if flush_level is None:
        flush_level = logging.ERROR
    if capacity is None:
        capacity = 100
    handler = MemoryHandler(capacity, flushLevel=flush_level, target=target_handler)

    def decorator(fn):
        def wrapper(*args, **kwargs):
            logger.addHandler(handler)
            try:
                return fn(*args, **kwargs)
            except Exception:
                logger.exception('call failed')
                raise
            finally:
                super(MemoryHandler, handler).flush()
                logger.removeHandler(handler)
        return wrapper

    return decorator

def write_line(s):
    sys.stderr.write('%s\n' % s)

def foo(fail=False):
    write_line('about to log at DEBUG ...')
    logger.debug('Actually logged at DEBUG')
    write_line('about to log at INFO ...')
    logger.info('Actually logged at INFO')
    write_line('about to log at WARNING ...')
    logger.warning('Actually logged at WARNING')
    if fail:
        write_line('about to log at ERROR ...')
        logger.error('Actually logged at ERROR')
        write_line('about to log at CRITICAL ...')
        logger.critical('Actually logged at CRITICAL')
    return fail

decorated_foo = log_if_errors(logger)(foo)

if __name__ == '__main__':
    logger.setLevel(logging.DEBUG)
    write_line('Calling undecorated foo with False')
    assert not foo(False)
    write_line('Calling undecorated foo with True')
    assert foo(True)
    write_line('Calling decorated foo with False')
    assert not decorated_foo(False)
    write_line('Calling decorated foo with True')
    assert decorated_foo(True)

Під час виконання цього сценарію має спостерігатися такий результат:

Calling undecorated foo with False
about to log at DEBUG ...
about to log at INFO ...
about to log at WARNING ...
Calling undecorated foo with True
about to log at DEBUG ...
about to log at INFO ...
about to log at WARNING ...
about to log at ERROR ...
about to log at CRITICAL ...
Calling decorated foo with False
about to log at DEBUG ...
about to log at INFO ...
about to log at WARNING ...
Calling decorated foo with True
about to log at DEBUG ...
about to log at INFO ...
about to log at WARNING ...
about to log at ERROR ...
Actually logged at DEBUG
Actually logged at INFO
Actually logged at WARNING
Actually logged at ERROR
about to log at CRITICAL ...
Actually logged at CRITICAL

Як ви можете бачити, фактичний вихід журналу відбувається лише тоді, коли реєструється подія, серйозність якої дорівнює ПОМИЛКІ або вище, але в цьому випадку також реєструються будь-які попередні події з нижчим рівнем серйозності.

Ви, звичайно, можете використовувати звичайні засоби декору:

@log_if_errors(logger)
def foo(fail=False):
    ...

Sending logging messages to email, with buffering

To illustrate how you can send log messages via email, so that a set number of messages are sent per email, you can subclass BufferingHandler. In the following example, which you can adapt to suit your specific needs, a simple test harness is provided which allows you to run the script with command line arguments specifying what you typically need to send things via SMTP. (Run the downloaded script with the -h argument to see the required and optional arguments.)

import logging
import logging.handlers
import smtplib

class BufferingSMTPHandler(logging.handlers.BufferingHandler):
    def __init__(self, mailhost, port, username, password, fromaddr, toaddrs,
                 subject, capacity):
        logging.handlers.BufferingHandler.__init__(self, capacity)
        self.mailhost = mailhost
        self.mailport = port
        self.username = username
        self.password = password
        self.fromaddr = fromaddr
        if isinstance(toaddrs, str):
            toaddrs = [toaddrs]
        self.toaddrs = toaddrs
        self.subject = subject
        self.setFormatter(logging.Formatter("%(asctime)s %(levelname)-5s %(message)s"))

    def flush(self):
        if len(self.buffer) > 0:
            try:
                smtp = smtplib.SMTP(self.mailhost, self.mailport)
                smtp.starttls()
                smtp.login(self.username, self.password)
                msg = "From: %s\r\nTo: %s\r\nSubject: %s\r\n\r\n" % (self.fromaddr, ','.join(self.toaddrs), self.subject)
                for record in self.buffer:
                    s = self.format(record)
                    msg = msg + s + "\r\n"
                smtp.sendmail(self.fromaddr, self.toaddrs, msg)
                smtp.quit()
            except Exception:
                if logging.raiseExceptions:
                    raise
            self.buffer = []

if __name__ == '__main__':
    import argparse

    ap = argparse.ArgumentParser()
    aa = ap.add_argument
    aa('host', metavar='HOST', help='SMTP server')
    aa('--port', '-p', type=int, default=587, help='SMTP port')
    aa('user', metavar='USER', help='SMTP username')
    aa('password', metavar='PASSWORD', help='SMTP password')
    aa('to', metavar='TO', help='Addressee for emails')
    aa('sender', metavar='SENDER', help='Sender email address')
    aa('--subject', '-s',
       default='Test Logging email from Python logging module (buffering)',
       help='Subject of email')
    options = ap.parse_args()
    logger = logging.getLogger()
    logger.setLevel(logging.DEBUG)
    h = BufferingSMTPHandler(options.host, options.port, options.user,
                             options.password, options.sender,
                             options.to, options.subject, 10)
    logger.addHandler(h)
    for i in range(102):
        logger.info("Info index = %d", i)
    h.flush()
    h.close()

If you run this script and your SMTP server is correctly set up, you should find that it sends eleven emails to the addressee you specify. The first ten emails will each have ten log messages, and the eleventh will have two messages. That makes up 102 messages as specified in the script.

Форматування часу за допомогою UTC (GMT) через налаштування

Sometimes you want to format times using UTC, which can be done using a class such as UTCFormatter, shown below:

import logging
import time

class UTCFormatter(logging.Formatter):
    converter = time.gmtime

і тоді ви можете використовувати UTCFormatter у своєму коді замість Formatter. Якщо ви хочете зробити це за допомогою конфігурації, ви можете використовувати dictConfig() API з підходом, проілюстрованим таким повним прикладом:

import logging
import logging.config
import time

class UTCFormatter(logging.Formatter):
    converter = time.gmtime

LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'formatters': {
        'utc': {
            '()': UTCFormatter,
            'format': '%(asctime)s %(message)s',
        },
        'local': {
            'format': '%(asctime)s %(message)s',
        }
    },
    'handlers': {
        'console1': {
            'class': 'logging.StreamHandler',
            'formatter': 'utc',
        },
        'console2': {
            'class': 'logging.StreamHandler',
            'formatter': 'local',
        },
    },
    'root': {
        'handlers': ['console1', 'console2'],
   }
}

if __name__ == '__main__':
    logging.config.dictConfig(LOGGING)
    logging.warning('The local time is %s', time.asctime())

Коли цей сценарій запускається, він має надрукувати щось на кшталт:

2015-10-17 12:53:29,501 The local time is Sat Oct 17 13:53:29 2015
2015-10-17 13:53:29,501 The local time is Sat Oct 17 13:53:29 2015

показує, як час форматується як місцевий час, так і UTC, по одному для кожного обробника.

Використання контекстного менеджера для вибіркового журналювання

Бувають випадки, коли було б корисно тимчасово змінити конфігурацію журналювання та повернути її назад після певних дій. Для цього менеджер контексту є найбільш очевидним способом збереження та відновлення контексту журналювання. Ось простий приклад такого менеджера контексту, який дозволяє вам за бажанням змінити рівень журналювання та додати обробник журналювання виключно в межах контекстного менеджера:

import logging
import sys

class LoggingContext:
    def __init__(self, logger, level=None, handler=None, close=True):
        self.logger = logger
        self.level = level
        self.handler = handler
        self.close = close

    def __enter__(self):
        if self.level is not None:
            self.old_level = self.logger.level
            self.logger.setLevel(self.level)
        if self.handler:
            self.logger.addHandler(self.handler)

    def __exit__(self, et, ev, tb):
        if self.level is not None:
            self.logger.setLevel(self.old_level)
        if self.handler:
            self.logger.removeHandler(self.handler)
        if self.handler and self.close:
            self.handler.close()
        # implicit return of None => don't swallow exceptions

Якщо ви вказуєте значення рівня, рівень реєстратора встановлюється на це значення в області блоку with, охопленого менеджером контексту. Якщо ви вкажете обробник, він додається до реєстратора під час входу до блоку та видаляється під час виходу з блоку. Ви також можете попросити менеджера закрити обробник для вас після виходу з блоку - ви можете зробити це, якщо вам більше не потрібен обробник.

Щоб проілюструвати, як це працює, ми можемо додати наступний блок коду до наведеного вище:

if __name__ == '__main__':
    logger = logging.getLogger('foo')
    logger.addHandler(logging.StreamHandler())
    logger.setLevel(logging.INFO)
    logger.info('1. This should appear just once on stderr.')
    logger.debug('2. This should not appear.')
    with LoggingContext(logger, level=logging.DEBUG):
        logger.debug('3. This should appear once on stderr.')
    logger.debug('4. This should not appear.')
    h = logging.StreamHandler(sys.stdout)
    with LoggingContext(logger, level=logging.DEBUG, handler=h, close=True):
        logger.debug('5. This should appear twice - once on stderr and once on stdout.')
    logger.info('6. This should appear just once on stderr.')
    logger.debug('7. This should not appear.')

Спочатку ми встановили рівень реєстратора на INFO, тому повідомлення №1 з’являється, а повідомлення №2 ні. Потім ми тимчасово змінюємо рівень на DEBUG у наступному блоці with, і тому з’являється повідомлення №3. Після виходу з блоку рівень реєстратора відновлюється до INFO, тому повідомлення №4 не з’являється. У наступному блоці with ми знову встановлюємо рівень DEBUG, але також додаємо запис обробника в sys.stdout. Таким чином, повідомлення №5 з’являється двічі на консолі (один раз через stderr і один раз через stdout). Після завершення оператора with статус залишається таким же, як і раніше, тому повідомлення №6 з’являється (як повідомлення №1), тоді як повідомлення №7 ні (так само, як повідомлення №2).

Якщо ми запустимо отриманий сценарій, результат буде таким:

$ python logctx.py
1. This should appear just once on stderr.
3. This should appear once on stderr.
5. This should appear twice - once on stderr and once on stdout.
5. This should appear twice - once on stderr and once on stdout.
6. This should appear just once on stderr.

Якщо ми запустимо його знову, але передаємо stderr до /dev/null, ми побачимо наступне, яке є єдиним повідомленням, яке записується в stdout:

$ python logctx.py 2>/dev/null
5. This should appear twice - once on stderr and once on stdout.

Ще раз, але передаючи stdout до /dev/null, ми отримуємо:

$ python logctx.py >/dev/null
1. This should appear just once on stderr.
3. This should appear once on stderr.
5. This should appear twice - once on stderr and once on stdout.
6. This should appear just once on stderr.

У цьому випадку повідомлення №5, надруковане в stdout, не з’являється, як очікувалося.

Звичайно, описаний тут підхід можна узагальнити, наприклад, тимчасово приєднати фільтри журналювання. Зверніть увагу, що наведений вище код працює як у Python 2, так і в Python 3.

Початковий шаблон програми CLI

Ось приклад, який показує, як можна:

  • Використовуйте рівень журналювання на основі аргументів командного рядка

  • Відправлення до кількох підкоманд в окремих файлах, усі журнали на одному рівні узгоджено

  • Використовуйте просту мінімальну конфігурацію

Припустімо, що у нас є програма командного рядка, завданням якої є зупинка, запуск або перезапуск деяких служб. Для ілюстрації це можна організувати як файл app.py, який є основним сценарієм для програми, з окремими командами, реалізованими в start.py, stop.py і restart.py. Припустимо далі, що ми хочемо контролювати докладність програми за допомогою аргументу командного рядка, за замовчуванням logging.INFO. Ось один із способів написання app.py:

import argparse
import importlib
import logging
import os
import sys

def main(args=None):
    scriptname = os.path.basename(__file__)
    parser = argparse.ArgumentParser(scriptname)
    levels = ('DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL')
    parser.add_argument('--log-level', default='INFO', choices=levels)
    subparsers = parser.add_subparsers(dest='command',
                                       help='Available commands:')
    start_cmd = subparsers.add_parser('start', help='Start a service')
    start_cmd.add_argument('name', metavar='NAME',
                           help='Name of service to start')
    stop_cmd = subparsers.add_parser('stop',
                                     help='Stop one or more services')
    stop_cmd.add_argument('names', metavar='NAME', nargs='+',
                          help='Name of service to stop')
    restart_cmd = subparsers.add_parser('restart',
                                        help='Restart one or more services')
    restart_cmd.add_argument('names', metavar='NAME', nargs='+',
                             help='Name of service to restart')
    options = parser.parse_args()
    # the code to dispatch commands could all be in this file. For the purposes
    # of illustration only, we implement each command in a separate module.
    try:
        mod = importlib.import_module(options.command)
        cmd = getattr(mod, 'command')
    except (ImportError, AttributeError):
        print('Unable to find the code for command \'%s\'' % options.command)
        return 1
    # Could get fancy here and load configuration from file or dictionary
    logging.basicConfig(level=options.log_level,
                        format='%(levelname)s %(name)s %(message)s')
    cmd(options)

if __name__ == '__main__':
    sys.exit(main())

А команди start, stop і restart можна реалізувати в окремих модулях, наприклад, для запуску:

# start.py
import logging

logger = logging.getLogger(__name__)

def command(options):
    logger.debug('About to start %s', options.name)
    # actually do the command processing here ...
    logger.info('Started the \'%s\' service.', options.name)

і, таким чином, для зупинки:

# stop.py
import logging

logger = logging.getLogger(__name__)

def command(options):
    n = len(options.names)
    if n == 1:
        plural = ''
        services = '\'%s\'' % options.names[0]
    else:
        plural = 's'
        services = ', '.join('\'%s\'' % name for name in options.names)
        i = services.rfind(', ')
        services = services[:i] + ' and ' + services[i + 2:]
    logger.debug('About to stop %s', services)
    # actually do the command processing here ...
    logger.info('Stopped the %s service%s.', services, plural)

і аналогічно для перезапуску:

# restart.py
import logging

logger = logging.getLogger(__name__)

def command(options):
    n = len(options.names)
    if n == 1:
        plural = ''
        services = '\'%s\'' % options.names[0]
    else:
        plural = 's'
        services = ', '.join('\'%s\'' % name for name in options.names)
        i = services.rfind(', ')
        services = services[:i] + ' and ' + services[i + 2:]
    logger.debug('About to restart %s', services)
    # actually do the command processing here ...
    logger.info('Restarted the %s service%s.', services, plural)

Якщо ми запустимо цю програму з рівнем журналу за замовчуванням, ми отримаємо такий результат:

$ python app.py start foo
INFO start Started the 'foo' service.

$ python app.py stop foo bar
INFO stop Stopped the 'foo' and 'bar' services.

$ python app.py restart foo bar baz
INFO restart Restarted the 'foo', 'bar' and 'baz' services.

Перше слово — це рівень журналювання, а друге — ім’я модуля або пакета місця, де було зареєстровано подію.

Якщо ми змінимо рівень журналювання, ми зможемо змінити інформацію, яка надсилається до журналу. Наприклад, якщо нам потрібна додаткова інформація:

$ python app.py --log-level DEBUG start foo
DEBUG start About to start foo
INFO start Started the 'foo' service.

$ python app.py --log-level DEBUG stop foo bar
DEBUG stop About to stop 'foo' and 'bar'
INFO stop Stopped the 'foo' and 'bar' services.

$ python app.py --log-level DEBUG restart foo bar baz
DEBUG restart About to restart 'foo', 'bar' and 'baz'
INFO restart Restarted the 'foo', 'bar' and 'baz' services.

А якщо ми хочемо менше:

$ python app.py --log-level WARNING start foo
$ python app.py --log-level WARNING stop foo bar
$ python app.py --log-level WARNING restart foo bar baz

У цьому випадку команди нічого не друкують на консолі, оскільки вони нічого не реєструють на рівні ПОПЕРЕДЖЕННЯ або вище.

Графічний інтерфейс Qt для журналювання

A question that comes up from time to time is about how to log to a GUI application. The Qt framework is a popular cross-platform UI framework with Python bindings using PySide2 or PyQt5 libraries.

У наступному прикладі показано, як увійти до Qt GUI. Це представляє простий клас QtHandler, який приймає callable, який має бути слотом у головному потоці, який виконує оновлення GUI. Робочий потік також створюється, щоб показати, як ви можете входити в графічний інтерфейс як з самого інтерфейсу користувача (за допомогою кнопки для ручного журналювання), так і з робочого потоку, який виконує роботу у фоновому режимі (тут просто реєструє повідомлення на випадкових рівнях з довільним короткі затримки між ними).

Робочий потік реалізовано за допомогою класу QThread Qt, а не модуля threading, оскільки є обставини, коли потрібно використовувати QThread, який забезпечує кращу інтеграцію з іншим Qt компоненти.

The code should work with recent releases of any of PySide6, PyQt6, PySide2 or PyQt5. You should be able to adapt the approach to earlier versions of Qt. Please refer to the comments in the code snippet for more detailed information.

import datetime
import logging
import random
import sys
import time

# Deal with minor differences between different Qt packages
try:
    from PySide6 import QtCore, QtGui, QtWidgets
    Signal = QtCore.Signal
    Slot = QtCore.Slot
except ImportError:
    try:
        from PyQt6 import QtCore, QtGui, QtWidgets
        Signal = QtCore.pyqtSignal
        Slot = QtCore.pyqtSlot
    except ImportError:
        try:
            from PySide2 import QtCore, QtGui, QtWidgets
            Signal = QtCore.Signal
            Slot = QtCore.Slot
        except ImportError:
            from PyQt5 import QtCore, QtGui, QtWidgets
            Signal = QtCore.pyqtSignal
            Slot = QtCore.pyqtSlot

logger = logging.getLogger(__name__)


#
# Signals need to be contained in a QObject or subclass in order to be correctly
# initialized.
#
class Signaller(QtCore.QObject):
    signal = Signal(str, logging.LogRecord)

#
# Output to a Qt GUI is only supposed to happen on the main thread. So, this
# handler is designed to take a slot function which is set up to run in the main
# thread. In this example, the function takes a string argument which is a
# formatted log message, and the log record which generated it. The formatted
# string is just a convenience - you could format a string for output any way
# you like in the slot function itself.
#
# You specify the slot function to do whatever GUI updates you want. The handler
# doesn't know or care about specific UI elements.
#
class QtHandler(logging.Handler):
    def __init__(self, slotfunc, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.signaller = Signaller()
        self.signaller.signal.connect(slotfunc)

    def emit(self, record):
        s = self.format(record)
        self.signaller.signal.emit(s, record)

#
# This example uses QThreads, which means that the threads at the Python level
# are named something like "Dummy-1". The function below gets the Qt name of the
# current thread.
#
def ctname():
    return QtCore.QThread.currentThread().objectName()


#
# Used to generate random levels for logging.
#
LEVELS = (logging.DEBUG, logging.INFO, logging.WARNING, logging.ERROR,
          logging.CRITICAL)

#
# This worker class represents work that is done in a thread separate to the
# main thread. The way the thread is kicked off to do work is via a button press
# that connects to a slot in the worker.
#
# Because the default threadName value in the LogRecord isn't much use, we add
# a qThreadName which contains the QThread name as computed above, and pass that
# value in an "extra" dictionary which is used to update the LogRecord with the
# QThread name.
#
# This example worker just outputs messages sequentially, interspersed with
# random delays of the order of a few seconds.
#
class Worker(QtCore.QObject):
    @Slot()
    def start(self):
        extra = {'qThreadName': ctname() }
        logger.debug('Started work', extra=extra)
        i = 1
        # Let the thread run until interrupted. This allows reasonably clean
        # thread termination.
        while not QtCore.QThread.currentThread().isInterruptionRequested():
            delay = 0.5 + random.random() * 2
            time.sleep(delay)
            try:
                if random.random() < 0.1:
                    raise ValueError('Exception raised: %d' % i)
                else:
                    level = random.choice(LEVELS)
                    logger.log(level, 'Message after delay of %3.1f: %d', delay, i, extra=extra)
            except ValueError as e:
                logger.exception('Failed: %s', e, extra=extra)
            i += 1

#
# Implement a simple UI for this cookbook example. This contains:
#
# * A read-only text edit window which holds formatted log messages
# * A button to start work and log stuff in a separate thread
# * A button to log something from the main thread
# * A button to clear the log window
#
class Window(QtWidgets.QWidget):

    COLORS = {
        logging.DEBUG: 'black',
        logging.INFO: 'blue',
        logging.WARNING: 'orange',
        logging.ERROR: 'red',
        logging.CRITICAL: 'purple',
    }

    def __init__(self, app):
        super().__init__()
        self.app = app
        self.textedit = te = QtWidgets.QPlainTextEdit(self)
        # Set whatever the default monospace font is for the platform
        f = QtGui.QFont('nosuchfont')
        if hasattr(f, 'Monospace'):
            f.setStyleHint(f.Monospace)
        else:
            f.setStyleHint(f.StyleHint.Monospace)  # for Qt6
        te.setFont(f)
        te.setReadOnly(True)
        PB = QtWidgets.QPushButton
        self.work_button = PB('Start background work', self)
        self.log_button = PB('Log a message at a random level', self)
        self.clear_button = PB('Clear log window', self)
        self.handler = h = QtHandler(self.update_status)
        # Remember to use qThreadName rather than threadName in the format string.
        fs = '%(asctime)s %(qThreadName)-12s %(levelname)-8s %(message)s'
        formatter = logging.Formatter(fs)
        h.setFormatter(formatter)
        logger.addHandler(h)
        # Set up to terminate the QThread when we exit
        app.aboutToQuit.connect(self.force_quit)

        # Lay out all the widgets
        layout = QtWidgets.QVBoxLayout(self)
        layout.addWidget(te)
        layout.addWidget(self.work_button)
        layout.addWidget(self.log_button)
        layout.addWidget(self.clear_button)
        self.setFixedSize(900, 400)

        # Connect the non-worker slots and signals
        self.log_button.clicked.connect(self.manual_update)
        self.clear_button.clicked.connect(self.clear_display)

        # Start a new worker thread and connect the slots for the worker
        self.start_thread()
        self.work_button.clicked.connect(self.worker.start)
        # Once started, the button should be disabled
        self.work_button.clicked.connect(lambda : self.work_button.setEnabled(False))

    def start_thread(self):
        self.worker = Worker()
        self.worker_thread = QtCore.QThread()
        self.worker.setObjectName('Worker')
        self.worker_thread.setObjectName('WorkerThread')  # for qThreadName
        self.worker.moveToThread(self.worker_thread)
        # This will start an event loop in the worker thread
        self.worker_thread.start()

    def kill_thread(self):
        # Just tell the worker to stop, then tell it to quit and wait for that
        # to happen
        self.worker_thread.requestInterruption()
        if self.worker_thread.isRunning():
            self.worker_thread.quit()
            self.worker_thread.wait()
        else:
            print('worker has already exited.')

    def force_quit(self):
        # For use when the window is closed
        if self.worker_thread.isRunning():
            self.kill_thread()

    # The functions below update the UI and run in the main thread because
    # that's where the slots are set up

    @Slot(str, logging.LogRecord)
    def update_status(self, status, record):
        color = self.COLORS.get(record.levelno, 'black')
        s = '<pre><font color="%s">%s</font></pre>' % (color, status)
        self.textedit.appendHtml(s)

    @Slot()
    def manual_update(self):
        # This function uses the formatted message passed in, but also uses
        # information from the record to format the message in an appropriate
        # color according to its severity (level).
        level = random.choice(LEVELS)
        extra = {'qThreadName': ctname() }
        logger.log(level, 'Manually logged!', extra=extra)

    @Slot()
    def clear_display(self):
        self.textedit.clear()


def main():
    QtCore.QThread.currentThread().setObjectName('MainThread')
    logging.getLogger().setLevel(logging.DEBUG)
    app = QtWidgets.QApplication(sys.argv)
    example = Window(app)
    example.show()
    if hasattr(app, 'exec'):
        rc = app.exec()
    else:
        rc = app.exec_()
    sys.exit(rc)

if __name__=='__main__':
    main()

Вхід до системного журналу з підтримкою RFC5424

Although RFC 5424 dates from 2009, most syslog servers are configured by default to use the older RFC 3164, which hails from 2001. When logging was added to Python in 2003, it supported the earlier (and only existing) protocol at the time. Since RFC5424 came out, as there has not been widespread deployment of it in syslog servers, the SysLogHandler functionality has not been updated.

RFC 5424 містить деякі корисні функції, такі як підтримка структурованих даних, і якщо вам потрібно мати можливість увійти на сервер системного журналу з його підтримкою, ви можете зробити це за допомогою обробника підкласів, який виглядає приблизно так:

import datetime
import logging.handlers
import re
import socket
import time

class SysLogHandler5424(logging.handlers.SysLogHandler):

    tz_offset = re.compile(r'([+-]\d{2})(\d{2})$')
    escaped = re.compile(r'([\]"\\])')

    def __init__(self, *args, **kwargs):
        self.msgid = kwargs.pop('msgid', None)
        self.appname = kwargs.pop('appname', None)
        super().__init__(*args, **kwargs)

    def format(self, record):
        version = 1
        asctime = datetime.datetime.fromtimestamp(record.created).isoformat()
        m = self.tz_offset.match(time.strftime('%z'))
        has_offset = False
        if m and time.timezone:
            hrs, mins = m.groups()
            if int(hrs) or int(mins):
                has_offset = True
        if not has_offset:
            asctime += 'Z'
        else:
            asctime += f'{hrs}:{mins}'
        try:
            hostname = socket.gethostname()
        except Exception:
            hostname = '-'
        appname = self.appname or '-'
        procid = record.process
        msgid = '-'
        msg = super().format(record)
        sdata = '-'
        if hasattr(record, 'structured_data'):
            sd = record.structured_data
            # This should be a dict where the keys are SD-ID and the value is a
            # dict mapping PARAM-NAME to PARAM-VALUE (refer to the RFC for what these
            # mean)
            # There's no error checking here - it's purely for illustration, and you
            # can adapt this code for use in production environments
            parts = []

            def replacer(m):
                g = m.groups()
                return '\\' + g[0]

            for sdid, dv in sd.items():
                part = f'[{sdid}'
                for k, v in dv.items():
                    s = str(v)
                    s = self.escaped.sub(replacer, s)
                    part += f' {k}="{s}"'
                part += ']'
                parts.append(part)
            sdata = ''.join(parts)
        return f'{version} {asctime} {hostname} {appname} {procid} {msgid} {sdata} {msg}'

Вам потрібно буде ознайомитися з RFC 5424, щоб повністю зрозуміти наведений вище код, і можливо, у вас є дещо інші потреби (наприклад, щодо того, як ви передаєте структурні дані в журнал). Тим не менш, наведене вище має адаптуватися до ваших конкретних потреб. За допомогою наведеного вище обробника ви передаєте структуровані дані, використовуючи щось на зразок цього:

sd = {
    'foo@12345': {'bar': 'baz', 'baz': 'bozz', 'fizz': r'buzz'},
    'foo@54321': {'rab': 'baz', 'zab': 'bozz', 'zzif': r'buzz'}
}
extra = {'structured_data': sd}
i = 1
logger.debug('Message %d', i, extra=extra)

How to treat a logger like an output stream

Sometimes, you need to interface to a third-party API which expects a file-like object to write to, but you want to direct the API’s output to a logger. You can do this using a class which wraps a logger with a file-like API. Here’s a short script illustrating such a class:

import logging

class LoggerWriter:
    def __init__(self, logger, level):
        self.logger = logger
        self.level = level

    def write(self, message):
        if message != '\n':  # avoid printing bare newlines, if you like
            self.logger.log(self.level, message)

    def flush(self):
        # doesn't actually do anything, but might be expected of a file-like
        # object - so optional depending on your situation
        pass

    def close(self):
        # doesn't actually do anything, but might be expected of a file-like
        # object - so optional depending on your situation. You might want
        # to set a flag so that later calls to write raise an exception
        pass

def main():
    logging.basicConfig(level=logging.DEBUG)
    logger = logging.getLogger('demo')
    info_fp = LoggerWriter(logger, logging.INFO)
    debug_fp = LoggerWriter(logger, logging.DEBUG)
    print('An INFO message', file=info_fp)
    print('A DEBUG message', file=debug_fp)

if __name__ == "__main__":
    main()

When this script is run, it prints

INFO:demo:An INFO message
DEBUG:demo:A DEBUG message

You could also use LoggerWriter to redirect sys.stdout and sys.stderr by doing something like this:

import sys

sys.stdout = LoggerWriter(logger, logging.INFO)
sys.stderr = LoggerWriter(logger, logging.WARNING)

You should do this after configuring logging for your needs. In the above example, the basicConfig() call does this (using the sys.stderr value before it is overwritten by a LoggerWriter instance). Then, you’d get this kind of result:

>>> print('Foo')
INFO:demo:Foo
>>> print('Bar', file=sys.stderr)
WARNING:demo:Bar
>>>

Of course, the examples above show output according to the format used by basicConfig(), but you can use a different formatter when you configure logging.

Note that with the above scheme, you are somewhat at the mercy of buffering and the sequence of write calls which you are intercepting. For example, with the definition of LoggerWriter above, if you have the snippet

sys.stderr = LoggerWriter(logger, logging.WARNING)
1 / 0

then running the script results in

WARNING:demo:Traceback (most recent call last):

WARNING:demo:  File "/home/runner/cookbook-loggerwriter/test.py", line 53, in <module>

WARNING:demo:
WARNING:demo:main()
WARNING:demo:  File "/home/runner/cookbook-loggerwriter/test.py", line 49, in main

WARNING:demo:
WARNING:demo:1 / 0
WARNING:demo:ZeroDivisionError
WARNING:demo::
WARNING:demo:division by zero

As you can see, this output isn’t ideal. That’s because the underlying code which writes to sys.stderr makes multiple writes, each of which results in a separate logged line (for example, the last three lines above). To get around this problem, you need to buffer things and only output log lines when newlines are seen. Let’s use a slghtly better implementation of LoggerWriter:

class BufferingLoggerWriter(LoggerWriter):
    def __init__(self, logger, level):
        super().__init__(logger, level)
        self.buffer = ''

    def write(self, message):
        if '\n' not in message:
            self.buffer += message
        else:
            parts = message.split('\n')
            if self.buffer:
                s = self.buffer + parts.pop(0)
                self.logger.log(self.level, s)
            self.buffer = parts.pop()
            for part in parts:
                self.logger.log(self.level, part)

This just buffers up stuff until a newline is seen, and then logs complete lines. With this approach, you get better output:

WARNING:demo:Traceback (most recent call last):
WARNING:demo:  File "/home/runner/cookbook-loggerwriter/main.py", line 55, in <module>
WARNING:demo:    main()
WARNING:demo:  File "/home/runner/cookbook-loggerwriter/main.py", line 52, in main
WARNING:demo:    1/0
WARNING:demo:ZeroDivisionError: division by zero

Шаблони, яких слід уникати

Незважаючи на те, що в попередніх розділах описано способи виконання речей, які вам можуть знадобитися, варто згадати деякі шаблони використання, які є некорисними і яких у більшості випадків слід уникати. Наступні розділи не мають певного порядку.

Відкриття одного файлу журналу кілька разів

У Windows зазвичай ви не зможете відкрити один і той же файл кілька разів, оскільки це призведе до помилки «файл використовується іншим процесом». Однак на платформах POSIX ви не отримаєте жодних помилок, якщо відкриєте той самий файл кілька разів. Це може бути зроблено випадково, наприклад:

  • Додавання обробника файлів більше одного разу, який посилається на той самий файл (наприклад, через помилку копіювання/вставлення/забути змінити).

  • Відкриття двох файлів, які виглядають по-різному, оскільки мають різні назви, але однакові, оскільки один є символічним посиланням на інший.

  • Розгалуження процесу, після якого і батьківський, і дочірній елементи мають посилання на той самий файл. Це може бути, наприклад, через використання модуля multiprocessing.

Багаторазове відкриття файлу може видаватись більшу частину часу ефективним, але на практиці це може призвести до ряду проблем:

  • Вихід журналу може бути спотвореним, оскільки кілька потоків або процесів намагаються записати в той самий файл. Хоча журналювання захищає від одночасного використання одного екземпляра обробника кількома потоками, такого захисту немає, якщо одночасний запис намагаються виконати два різні потоки, використовуючи два різні екземпляри обробника, які випадково вказують на той самий файл.

  • An attempt to delete a file (e.g. during file rotation) silently fails, because there is another reference pointing to it. This can lead to confusion and wasted debugging time - log entries end up in unexpected places, or are lost altogether. Or a file that was supposed to be moved remains in place, and grows in size unexpectedly despite size-based rotation being supposedly in place.

Використовуйте методи, описані в Реєстрація в один файл з кількох процесів, щоб уникнути таких проблем.

Використання реєстраторів як атрибутів у класі або передача їх як параметрів

Хоча можуть бути незвичайні випадки, коли вам знадобиться це зробити, загалом це не має сенсу, оскільки реєстратори є одиночними. Код завжди може отримати доступ до певного екземпляра реєстратора за назвою за допомогою logging.getLogger(name), тому передавати екземпляри та зберігати їх як атрибути екземпляра безглуздо. Зверніть увагу, що в інших мовах, таких як Java і C#, реєстратори часто є статичними атрибутами класу. Однак цей шаблон не має сенсу в Python, де модуль (а не клас) є одиницею декомпозиції програмного забезпечення.

Adding handlers other than NullHandler to a logger in a library

Налаштування журналювання шляхом додавання обробників, засобів форматування та фільтрів є відповідальністю розробника програми, а не розробника бібліотеки. Якщо ви підтримуєте бібліотеку, переконайтеся, що ви не додаєте обробники до жодного з ваших журналів, крім екземпляра NullHandler.

Створення великої кількості журналів

Реєстратори — це одиночні елементи, які ніколи не звільняються під час виконання сценарію, тому створення великої кількості реєстраторів буде використовувати пам’ять, яку потім не можна звільнити. Замість створення реєстратора, наприклад, оброблено файл або встановлено мережеве з’єднання, скористайтеся існуючими механізмами для передачі контекстної інформації у ваші журнали та обмежте реєстратори, створені тими, що описують області у вашій програмі (зазвичай модулі, але іноді трохи більш дрібнозернисті, ніж це) .

Інші ресурси

Дивись також

Модуль logging

Довідник API для модуля журналювання.

Модуль logging.config

API конфігурації для модуля журналювання.

Модуль logging.handlers

Корисні обробники, включені в модуль журналювання.

Основний посібник

Розширений посібник