]> git.evergreen-ils.org Git - OpenSRF.git/blob - src/python/osrf/log.py
f0ef2ade1e704f9ef648e928bd6973e6871eb552
[OpenSRF.git] / src / python / osrf / log.py
1 # -----------------------------------------------------------------------
2 # Copyright (C) 2007  Georgia Public Library Service
3 # Bill Erickson <billserickson@gmail.com>
4
5 # This program is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU General Public License
7 # as published by the Free Software Foundation; either version 2
8 # of the License, or (at your option) any later version.
9
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 # GNU General Public License for more details.
14 # -----------------------------------------------------------------------
15
16 import traceback, sys, os, re
17 from osrf.const import *
18
19 loglevel = 4
20
21 def osrfInitLog(level, facility=None, file=None):
22     """Initialize the logging subsystem."""
23     import syslog
24     global loglevel
25     if facility: osrfInitSyslog(facility, level)
26     loglevel = level
27     syslog.syslog(LOG_DEBUG, "syslog initialized")
28
29
30 # -----------------------------------------------------------------------
31 # Define wrapper functions for the log levels
32 # -----------------------------------------------------------------------
33 def osrfLogInternal(s): __osrfLog(OSRF_LOG_INTERNAL,s)
34 def osrfLogDebug(s): __osrfLog(OSRF_LOG_DEBUG,s)
35 def osrfLogInfo(s): __osrfLog(OSRF_LOG_INFO,s)
36 def osrfLogWarn(s): __osrfLog(OSRF_LOG_WARN,s)
37 def osrfLogErr(s): __osrfLog(OSRF_LOG_ERR,s)
38
39
40 frgx = re.compile('/.*/')
41
42 def __osrfLog(level, msg):
43     """Builds the log message and passes the message off to the logger."""
44
45     try:
46         import syslog
47     except:
48         return
49         
50     global loglevel
51     if int(level) > int(loglevel): return
52
53     # find the caller info for logging the file and line number
54     tb = traceback.extract_stack(limit=3)
55     tb = tb[0]
56     lvl = 'DEBG'
57     slvl = syslog.LOG_DEBUG
58
59     if level == OSRF_LOG_INTERNAL: lvl = 'INT '; slvl=syslog.LOG_DEBUG
60     if level == OSRF_LOG_INFO: lvl = 'INFO'; slvl=syslog.LOG_INFO
61     if level == OSRF_LOG_WARN: lvl = 'WARN'; slvl=syslog.LOG_WARNING
62     if level == OSRF_LOG_ERR:  lvl = 'ERR '; slvl=syslog.LOG_ERR
63
64     file = frgx.sub('',tb[0])
65     msg = '[%s:%d:%s:%s] %s' % (lvl, os.getpid(), file, tb[1], msg)
66     syslog.syslog(slvl, msg)
67
68     if level == OSRF_LOG_ERR:
69         sys.stderr.write(msg + '\n')
70
71
72 def osrfInitSyslog(facility, level):
73     """Connect to syslog and set the logmask based on the level provided."""
74
75     level = int(level)
76
77     if facility == 'local0': facility = syslog.LOG_LOCAL0
78     if facility == 'local1': facility = syslog.LOG_LOCAL1
79     if facility == 'local2': facility = syslog.LOG_LOCAL2
80     if facility == 'local3': facility = syslog.LOG_LOCAL3
81     if facility == 'local4': facility = syslog.LOG_LOCAL4
82     if facility == 'local5': facility = syslog.LOG_LOCAL5
83     if facility == 'local6': facility = syslog.LOG_LOCAL6
84     # XXX add other facility maps if necessary
85     openlog(sys.argv[0], 0, facility)
86
87     # this is redundant...
88     mask = LOG_UPTO(syslog.LOG_ERR)
89     if level >= 1: mask |= LOG_MASK(syslog.LOG_WARNING)
90     if level >= 2: mask |= LOG_MASK(syslog.LOG_NOTICE)
91     if level >= 3: mask |= LOG_MASK(syslog.LOG_INFO)
92     if level >= 4: mask |= LOG_MASK(syslog.LOG_DEBUG)
93     syslog.setlogmask(mask)
94