1 # -----------------------------------------------------------------------
2 # Copyright (C) 2007 Georgia Public Library Service
3 # Bill Erickson <billserickson@gmail.com>
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.
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 # -----------------------------------------------------------------------
16 import traceback, sys, os, re, threading, time
17 from osrf.const import OSRF_LOG_DEBUG, OSRF_LOG_ERR, OSRF_LOG_INFO, \
18 OSRF_LOG_INTERNAL, OSRF_LOG_TYPE_FILE, OSRF_LOG_TYPE_STDERR, \
19 OSRF_LOG_TYPE_SYSLOG, OSRF_LOG_WARN
20 LOG_SEMAPHORE = threading.BoundedSemaphore(value=1)
23 LOG_LEVEL = OSRF_LOG_DEBUG
24 LOG_TYPE = OSRF_LOG_TYPE_STDERR
26 FRGX = re.compile('/.*/')
28 _xid = '' # the current XID
29 _xid_pfx = '' # our XID prefix
31 _xid_is_client = False # true if we are the request origin
34 def initialize(level, facility=None, logfile=None, is_client=False):
35 """Initialize the logging subsystem."""
36 global LOG_LEVEL, LOG_TYPE, LOG_FILE, _xid_is_client
38 _xid_is_client = is_client
45 sys.stderr.write("syslog not found, logging to stderr\n")
48 LOG_TYPE = OSRF_LOG_TYPE_SYSLOG
49 initialize_syslog(facility, level)
53 LOG_TYPE = OSRF_LOG_TYPE_FILE
57 global _xid, _xid_pfx, _xid_is_client, _xid_ctr
60 _xid_pfx = "%s%s" % (time.time(), os.getpid())
61 _xid = "%s%d" % (_xid_pfx, _xid_ctr)
75 # -----------------------------------------------------------------------
76 # Define wrapper functions for the log levels
77 # -----------------------------------------------------------------------
78 def log_internal(debug_str):
79 __log(OSRF_LOG_INTERNAL, debug_str)
80 def log_debug(debug_str):
81 __log(OSRF_LOG_DEBUG, debug_str)
82 def log_info(debug_str):
83 __log(OSRF_LOG_INFO, debug_str)
84 def log_warn(debug_str):
85 __log(OSRF_LOG_WARN, debug_str)
86 def log_error(debug_str):
87 __log(OSRF_LOG_ERR, debug_str)
89 def __log(level, msg):
90 """Builds the log message and passes the message off to the logger."""
91 global LOG_LEVEL, LOG_TYPE
96 if level == OSRF_LOG_ERR:
97 sys.stderr.write('ERR ' + msg)
100 if int(level) > int(LOG_LEVEL): return
102 # find the caller info for logging the file and line number
103 tb = traceback.extract_stack(limit=3)
107 if level == OSRF_LOG_INTERNAL:
109 if level == OSRF_LOG_INFO:
111 if level == OSRF_LOG_WARN:
113 if level == OSRF_LOG_ERR:
116 filename = FRGX.sub('', tb[0])
117 msg = '[%s:%d:%s:%s:%s:%s] %s' % (lvl, os.getpid(), filename, tb[1], threading.currentThread().getName(), _xid, msg)
119 if LOG_TYPE == OSRF_LOG_TYPE_SYSLOG:
120 __log_syslog(level, msg)
122 if LOG_TYPE == OSRF_LOG_TYPE_FILE:
125 sys.stderr.write("%s\n" % msg)
127 if level == OSRF_LOG_ERR and LOG_TYPE != OSRF_LOG_TYPE_STDERR:
128 sys.stderr.write(msg + '\n')
130 def __log_syslog(level, msg):
131 ''' Logs the message to syslog '''
134 slvl = syslog.LOG_DEBUG
135 if level == OSRF_LOG_INTERNAL:
136 slvl = syslog.LOG_DEBUG
137 if level == OSRF_LOG_INFO:
138 slvl = syslog.LOG_INFO
139 if level == OSRF_LOG_WARN:
140 slvl = syslog.LOG_WARNING
141 if level == OSRF_LOG_ERR:
142 slvl = syslog.LOG_ERR
144 syslog.syslog(slvl, msg)
147 ''' Logs the message to a file. '''
152 timestamp = time.strftime('%Y-%M-%d %H:%m:%S', time.localtime(epoch))
153 timestamp += '.%s' % str('%0.3f' % epoch)[-3:] # grab the millis
157 logfile = open(LOG_FILE, 'a')
159 sys.stderr.write("cannot open log file for writing: %s\n" % LOG_FILE)
160 LOG_TYPE = OSRF_LOG_TYPE_STDERR
163 LOG_SEMAPHORE.acquire()
164 logfile.write("%s %s\n" % (timestamp, msg))
166 LOG_SEMAPHORE.release()
170 def initialize_syslog(facility, level):
171 """Connect to syslog and set the logmask based on the level provided."""
176 if facility == 'local0':
177 facility = syslog.LOG_LOCAL0
178 if facility == 'local1':
179 facility = syslog.LOG_LOCAL1
180 if facility == 'local2':
181 facility = syslog.LOG_LOCAL2
182 if facility == 'local3':
183 facility = syslog.LOG_LOCAL3
184 if facility == 'local4':
185 facility = syslog.LOG_LOCAL4
186 if facility == 'local5':
187 facility = syslog.LOG_LOCAL5
188 if facility == 'local6':
189 facility = syslog.LOG_LOCAL6
190 # add other facility maps if necessary...
192 syslog.openlog(sys.argv[0], 0, facility)