import repository from arizona
[raven.git] / lib / ravenlib / ravenlog.py
1 # A simple wrapper around python's logging
2
3 import sys
4 import logging
5
6 DEFAULT_STDOUT_FORMAT="%(message)s"
7
8 class RavenLog:
9     def __init__(self, level = logging.INFO, stdoutStream=sys.stderr, stdoutFormatString=DEFAULT_STDOUT_FORMAT):
10         self.initLogger(stdoutStream, stdoutFormatString)
11         self.setStdoutLevel(level)
12
13     def setStdoutStream(self, stream):
14         if self.stdout_handler:
15             self.stdout_handler.stream = stream
16
17     def getStdoutStream(self):
18         if self.stdout_handler:
19             return self.stdout_handler.stream
20         else:
21             return None
22
23     def getStdoutLevel(self):
24         if self.stdout_handler:
25             return self.stdout_handler.level
26         else:
27             return Logging.INFO
28
29     def setStdoutLevel(self, level):
30         self.logger.setLevel(level)
31         if self.stdout_handler:
32             self.stdout_handler.setLevel(level)
33
34     def cloneStdoutConfig(self, fromObject):
35         """ clones the stdout settings from one RavenLog into this RavenLog """
36         self.setStdoutLevel(fromObject.getStdoutLevel())
37         self.setStdoutStream(fromObject.getStdoutStream())
38
39     def initLogger(self, stdoutStream, stdoutFormatString):
40         self.loggerName = "ravenlog." + self.__class__.__name__
41         # It says we're supposed to use logging.getLogger() rather than
42         # instantiating logger objects directly. Doing the former causes a
43         # number of problems such as inheritance issues, conflicts, etc. So,
44         # we do the latter.
45         self.logger = logging.Logger(self.loggerName)
46         self.stdout_handler = self.initHandler(stdoutStream, stdoutFormatString)
47         self.logger.addHandler(self.stdout_handler)
48
49     def initHandler(self, stream, formatString):
50         formatter = logging.Formatter(formatString)
51         handler = logging.StreamHandler(stream)
52         handler.setFormatter(formatter)
53         return handler
54
55     def PrintComma(self, *args):
56         print >> self.stdout_handler.stream, " ".join(args) + " ",
57
58     # has to be "Print", because "print" is a reserved word
59     def Print(self, *args):
60         args = [str(arg) for arg in args]
61         self.logger.info(" ".join(args))
62
63     def debug(self, msg, *args, **kargs):
64         self.logger.debug(msg, *args, **kargs)
65
66     def info(self, msg, *args, **kargs):
67         self.logger.info(msg, *args, **kargs)
68
69     def warning(self, msg, *args, **kargs):
70         self.logger.warning(msg, *args, **kargs)
71
72     def error(self, msg, *args, **kargs):
73         self.logger.error(msg, *args, **kargs)
74
75     def critical(self, msg, *args, **kargs):
76         self.logger.critical(msg, *args, **kargs)
77
78 if __name__=="__main__":
79     l = RavenLog()
80
81     # "This is a print statement"
82     l.Print("This", "is", "a", "print", "statement")
83
84     # "foo bar"
85     l.PrintComma("foo")
86     l.Print("bar")
87
88     # "This is an info message"
89     l.info("This %s %s %s %s", "is", "an", "info", "message")
90
91     # blank line
92     l.Print()
93
94     # multiline message
95     l.info("Line 1\nLine 2\nLine 3")
96