Imported Upstream version 12.1.0
[contrib/python-twisted.git] / doc / core / examples / testlogging.py
1 # Copyright (c) Twisted Matrix Laboratories.
2 # See LICENSE for details.
3
4
5 """Test logging.
6
7 Message should only be printed second time around.
8 """
9
10 from twisted.python import log
11 from twisted.internet import reactor
12
13 import sys, warnings
14
15 def test(i):
16     print "printed", i
17     log.msg("message %s" % i)
18     warnings.warn("warning %s" % i)
19     try:
20         raise RuntimeError, "error %s" % i
21     except:
22         log.err()
23
24 def startlog():
25     log.startLogging(sys.stdout)
26
27 def end():
28     reactor.stop()
29
30 # pre-reactor run
31 test(1)
32
33 # after reactor run
34 reactor.callLater(0.1, test, 2)
35 reactor.callLater(0.2, startlog)
36
37 # after startLogging
38 reactor.callLater(0.3, test, 3)
39 reactor.callLater(0.4, end)
40
41 reactor.run()