exception_logger.py 5.82 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#
#  engine
#
#  Playout Daemon for autoradio project
#
#
#  Copyright (C) 2017-2018 Gottfried Gaisbauer <gottfried.gaisbauer@servus.at>
#
#  This file is part of engine.
#
#  engine is free software: you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation, either version 3 of the License, or
#  any later version.
#
#  engine is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with engine. If not, see <http://www.gnu.org/licenses/>.
#

25
import simplejson
26
import logging
27

28

29
class ExceptionLogger:
30
    logger = None
31
    error_data = None
32
33
34
35
    job_result = None

    def __init__(self):
        self.logger = logging.getLogger("AuraEngine")
36

37
38
39
40
41
42
43
    # ------------------------------------------------------------------------------------------ #
    def __get_error__(self, job, errornumber, data):
        """
        Privat: Ermittelt Fehlermeldung, Job-Name (Klassenmethode) und Fehlercode für den Job aus error/controller_error.js
        @type errornumber:  string
        @param errornumber: Die interne Fehlernummer der aufrufenden Methode
        """
44

45
        if data is None:
46
47
48
49
            data = {}
        if type(data) == type(str()):
            data = simplejson.loads(data)

50
        has_data = isinstance(data, (dict)) and len(data) > 0
51

52
53
54
        if job in self.error_data:
            err_msg = self.error_data[job][errornumber]
            err_id = self.error_data[job]['id'] + str(errornumber)
55
            if has_data:
56
                for key in data.keys():
57
58
                    err_msg = err_msg.replace('::' + key + '::', str(data[key]))
            data['message'] = err_msg
59
            data['job'] = job
60
61
            data['code'] = err_id

62
63
64
        return data

    # ------------------------------------------------------------------------------------------ #
65
    def success(self, job, data=None, errnum='00', value=''):
66
67
68
69
70
71
72
73
74
75
        """
        Erfolgsmeldung loggen
        @type errnum:    string
        @param errnum:   Errornummer der aufrufenden Funktion
        @type value:     string
        @param value:    Optionaler Wert
        @type section:   string
        @param section:  Gültigkeitsbereich
        """
        error = self.__get_error__(job, errnum, data)
76
77
78
79
80
81
        self.job_result = {'message': error['message'],
                           'code': error['code'],
                           'success': 'success',
                           'job': error['job'],
                           'value': value}
        self.logger.debug(job + " successfully done " + str(self.job_result))
82
83

    # ------------------------------------------------------------------------------------------ #
84
    def info(self, job, data=None, errnum='01', value=''):
85
86
87
88
89
90
91
92
93
94
        """
        Info loggen
        @type errnum:    string
        @param errnum:   Errornummer der aufrufenden Funktion
        @type value:     string
        @param value:    Optionaler Wert
        @type section:   string
        @param section:  Gültigkeitsbereich
        """
        error = self.__get_error__(job, errnum, data)
95
96
97
98
99
100
101
        self.job_result = {'message': error['message'],
                           'code': error['code'],
                           'success': 'info',
                           'job': error['job'],
                           'value': value}
        self.logger.info(str(self.job_result))

102
103

    # ------------------------------------------------------------------------------------------ #
104
    def warning(self, job, data=None, errnum='01', value=''):
105
106
107
108
109
110
111
112
        """
        Warnung loggen
        @type errnum:    string
        @param errnum:   Errornummer der aufrufenden Funktion
        @type value:     string
        @param value:    Optionaler Wert
        """
        error = self.__get_error__(job, errnum, data)
113
114
115
116
117
118
        self.job_result = {'message': error['message'],
                           'code': error['code'],
                           'success': 'warning',
                           'job': error['job'],
                           'value': value}
        self.logger.warning(str(self.job_result))
119
120

    # ------------------------------------------------------------------------------------------ #
121
    def error(self, job, data=None, errnum='01', value=''):
122
123
124
125
126
127
128
129
        """
        Error loggen
        @type errnum:    string
        @param errnum:   Errornummer der aufrufenden Funktion
        @type value:     string
        @param value:    Optionaler Wert
        """
        error = self.__get_error__(job, errnum, data)
130
131
132
133
134
135
        self.job_result = {'message': error['message'],
                           'code': error['code'],
                           'success': 'error',
                           'job': error['job'],
                           'value': value}
        self.logger.error(str(self.job_result))
136
137
138
139
140
141
142
143
144
145
146
147
148

    # ------------------------------------------------------------------------------------------ #
    def fatal(self, job, data=None, errnum='01', value='', section='execjob'):
        """
        Fatal error loggen
        @type errnum:    string
        @param errnum:   Errornummer der aufrufenden Funktion
        @type value:     string
        @param value:    Optionaler Wert
        @type section:   string
        @param section:  Gültigkeitsbereich
        """
        error = self.__get_error__(job, errnum, data)
149
150
151
152
153
154
155
        self.job_result = {'message': error['message'],
                           'code': error['code'],
                           'success': 'fatal',
                           'job': error['job'],
                           'value': value,
                           'section': section}
        self.logger.critical(str(self.job_result))