log.py
1.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# -*- coding: utf-8 -*-
__author__ = 'hubian'
import logging
from os.path import realpath, dirname
from logging import config, DEBUG, INFO
class Log(object):
"""Wrapper of Python logging module for easier usage
:Example:
from hackathon.log import log
log.info("message of INFO level ")
log.error(exception) # where exception is of type Exception or it inheritances
.. notes:: make sure directory '/var/log/open-hackathon/' exists and accessible
"""
def debug(self, debug):
"""write message into log file with DEBUG level
:type debug: str|unicode
:param debug: message to write
"""
if self.logger.isEnabledFor(DEBUG):
self.logger.debug(debug)
def info(self, info):
"""write message into log file with INFO level
:type info: str|unicode
:param info: message to write
"""
if self.logger.isEnabledFor(INFO):
self.logger.info(info)
def warn(self, warn):
"""write message into log file with WARN level
:type warn: str|unicode
:param warn: message to write
"""
self.logger.warn(warn)
def error(self, exception):
"""write exception message and stack trace into log file with ERROR level
:type exception: Exception
:param exception: exception to write
"""
self.logger.error(str(exception), exc_info=1)
def critical(self, critical):
"""write message into log file with FATAL level
:type critical: str|unicode
:param critical: message to write
"""
self.logger.critical(critical)
def __init__(self):
"""initialize the log wrapper through 'logging.conf' file which should be in the same dir of this file"""
logging.config.fileConfig("%s/logging.conf" % dirname(realpath(__file__)))
self.logger = logging.getLogger("myLogger")