Debian Codegraph @

docs/core/examples/twistd-logging.tac

raw
1 # Invoke this script with:
2
3 # $ twistd -ny twistd-logging.tac
4
5 # It will create a log file named "twistd-logging.log". The log file will
6 # be formatted such that each line contains the representation of the dict
7 # structure of each log message.
8
9 from twisted.application.service import Application
10 from twisted.internet.task import LoopingCall
11 from twisted.python.log import ILogObserver, msg
12 from twisted.python.util import untilConcludes
13
14 logfile = open("twistd-logging.log", "a")
15
16
17 def log(eventDict):
18 # untilConcludes is necessary to retry the operation when the system call
19 # has been interrupted.
20 untilConcludes(logfile.write, f"Got a log! {eventDict}\n")
21 untilConcludes(logfile.flush)
22
23
24 def logSomething():
25 msg("A log message")
26
27
28 LoopingCall(logSomething).start(1)
29
30 application = Application("twistd-logging")
31 application.setComponent(ILogObserver, log)