File: twistd-logging.tac

package info (click to toggle)
twisted 25.5.0-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 20,560 kB
  • sloc: python: 203,171; makefile: 200; sh: 92; javascript: 36; xml: 31
file content (31 lines) | stat: -rw-r--r-- 882 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# Invoke this script with:

# $ twistd -ny twistd-logging.tac

# It will create a log file named "twistd-logging.log".  The log file will
# be formatted such that each line contains the representation of the dict
# structure of each log message.

from twisted.application.service import Application
from twisted.internet.task import LoopingCall
from twisted.python.log import ILogObserver, msg
from twisted.python.util import untilConcludes

logfile = open("twistd-logging.log", "a")


def log(eventDict):
    # untilConcludes is necessary to retry the operation when the system call
    # has been interrupted.
    untilConcludes(logfile.write, f"Got a log! {eventDict}\n")
    untilConcludes(logfile.flush)


def logSomething():
    msg("A log message")


LoopingCall(logSomething).start(1)

application = Application("twistd-logging")
application.setComponent(ILogObserver, log)