File: modjy_log.py

package info (click to toggle)
jython 2.5.3-16%2Bdeb9u1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 43,772 kB
  • ctags: 106,434
  • sloc: python: 351,322; java: 216,349; xml: 1,584; sh: 330; perl: 114; ansic: 102; makefile: 45
file content (80 lines) | stat: -rw-r--r-- 2,133 bytes parent folder | download | duplicates (7)
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
###
#
# Copyright Alan Kennedy. 
# 
# You may contact the copyright holder at this uri:
# 
# http://www.xhaus.com/contact/modjy
# 
# The licence under which this code is released is the Apache License v2.0.
# 
# The terms and conditions of this license are listed in a file contained
# in the distribution that also contained this file, under the name
# LICENSE.txt.
# 
# You may also read a copy of the license at the following web address.
# 
# http://modjy.xhaus.com/LICENSE.txt
#
###

import java

import sys

DEBUG = 'debug'
INFO = 'info'
WARN = 'warn'
ERROR = 'error'
FATAL = 'fatal'

levels_dict = {}
ix = 0
for level in [DEBUG, INFO, WARN, ERROR, FATAL, ]:
    levels_dict[level]=ix
    ix += 1

class modjy_logger:

    def __init__(self, context):
        self.log_ctx = context
        self.format_str = "%(lvl)s:\t%(msg)s"
        self.log_level = levels_dict[DEBUG]

    def _log(self, level, level_str, msg, exc):
        if level >= self.log_level:
            msg = self.format_str % {'lvl': level_str, 'msg': msg, }
            if exc:
#                java.lang.System.err.println(msg, exc)
                self.log_ctx.log(msg, exc)
            else:
#                java.lang.System.err.println(msg)
                self.log_ctx.log(msg)

    def debug(self, msg, exc=None):
        self._log(0, DEBUG, msg, exc)

    def info(self, msg, exc=None):
        self._log(1, INFO, msg, exc)

    def warn(self, msg, exc=None):
        self._log(2, WARN, msg, exc)

    def error(self, msg, exc=None):
        self._log(3, ERROR, msg, exc)

    def fatal(self, msg, exc=None):
        self._log(4, FATAL, msg, exc)

    def set_log_level(self, level_string):
        try:
            self.log_level = levels_dict[level_string]
        except KeyError:
            raise BadParameter("Invalid log level: '%s'" % level_string)

    def set_log_format(self, format_string):
        # BUG! Format string never actually used in this function.
        try:
            self._log(debug, "This is a log formatting test", None)
        except KeyError:
            raise BadParameter("Bad format string: '%s'" % format_string)