Commit f4daeb3e authored by Devaev Maxim's avatar Devaev Maxim

Added syslog support for logger

parent c579cd1e
...@@ -3,20 +3,25 @@ ...@@ -3,20 +3,25 @@
import sys import sys
import traceback import traceback
import syslog
import const import const
import config import config
##### Public constants ##### ##### Public constants #####
ERROR_MESSAGE = 0 ERROR_MESSAGE = (0, syslog.LOG_ERR, const.LOG_LEVEL_INFO)
INFO_MESSAGE = 1 WARNING_MESSAGE = (1, syslog.LOG_WARNING, const.LOG_LEVEL_INFO)
VERBOSE_MESSAGE = 2 NOTICE_MESSAGE = (2, syslog.LOG_NOTICE, const.LOG_LEVEL_INFO)
DEBUG_MESSAGE = 3 INFO_MESSAGE = (3, syslog.LOG_INFO, const.LOG_LEVEL_INFO)
VERBOSE_MESSAGE = (4, syslog.LOG_INFO, const.LOG_LEVEL_VERBOSE)
DEBUG_MESSAGE = (5, syslog.LOG_INFO, const.LOG_LEVEL_DEBUG) # syslog.LOG_DEBUG
ALL_MESSAGES_LIST = ( ALL_MESSAGES_LIST = (
ERROR_MESSAGE, ERROR_MESSAGE,
INFO_MESSAGE, INFO_MESSAGE,
NOTICE_MESSAGE,
WARNING_MESSAGE,
VERBOSE_MESSAGE, VERBOSE_MESSAGE,
DEBUG_MESSAGE DEBUG_MESSAGE
) )
...@@ -29,25 +34,19 @@ class UnknownMessageType(Exception) : ...@@ -29,25 +34,19 @@ class UnknownMessageType(Exception) :
##### Public methods ##### ##### Public methods #####
def message(message_type, message) : def message(message_type, message) :
if message_type in (ERROR_MESSAGE, INFO_MESSAGE) : if not message_type in ALL_MESSAGES_LIST :
message_level = const.LOG_LEVEL_INFO
elif message_type == VERBOSE_MESSAGE :
message_level = const.LOG_LEVEL_VERBOSE
elif message_type == DEBUG_MESSAGE :
message_level = const.LOG_LEVEL_DEBUG
else :
raise UnknownMessageType("Message type \"%d\" not in list %s" % (message_type, ALL_MESSAGES_LIST)) raise UnknownMessageType("Message type \"%d\" not in list %s" % (message_type, ALL_MESSAGES_LIST))
if message_level <= config.value(const.MY_NAME, "log_level") : if message_type[2] <= config.value(const.MY_NAME, "log_level") :
message_level_prefixes_list = ( message_type_texts_list = ("Error", "Warning", "Notice", "Info", "Details", "Debug")
"%s [ Error ]:" % (const.MY_NAME), message = "[ %s ]: %s" % (message_type_texts_list[message_type[0]], message)
"%s [ Info ]:" % (const.MY_NAME),
"%s [ Details ]:" % (const.MY_NAME),
"%s [ Debug ]:" % (const.MY_NAME)
)
print >> sys.stderr, message_level_prefixes_list[message_type], message
def attachException() : print >> sys.stderr, const.MY_NAME, message
if config.value(const.RUNTIME, "use_syslog") :
syslog.syslog(message_type[1], message)
def attachException(message_type = ERROR_MESSAGE) :
for line in traceback.format_exc().splitlines() : for line in traceback.format_exc().splitlines() :
message(ERROR_MESSAGE, line) message(message_type, line)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment