File: logs.py

package info (click to toggle)
zabbix-cli 1.7.0-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 532 kB
  • ctags: 353
  • sloc: python: 6,051; sh: 323; makefile: 17
file content (60 lines) | stat: -rw-r--r-- 1,944 bytes parent folder | download
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
#!/usr/bin/env python
#
# Authors:
# rafael@postgresql.org.es / http://www.postgresql.org.es/
#
# Copyright (c) 2014-2015 USIT-University of Oslo
#
# This file is part of Zabbix-CLI
# https://github.com/rafaelma/zabbix-cli
#
# Zabbix-CLI 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
# (at your option) any later version.
#
# Zabbix-CLI 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 Zabbix-CLI.  If not, see <http://www.gnu.org/licenses/>.

import os
import sys
import logging

from zabbix_cli.config import *

class log(logging.Logger):

    # ############################################
    # Constructor    
    # ############################################

    def __init__(self, logger_name,config_file):
        """ The Constructor."""
     
        self.logger_name = logger_name
        self.conf = configuration(config_file)
        
        self.logger = logging.getLogger(logger_name)
        level = logging.getLevelName(self.conf.log_level.upper())
        
        self.logger.setLevel(level)
    
        try:

            self.fh = logging.FileHandler(self.conf.log_file)
            self.fh.setLevel(level)
            
            self.formatter = logging.Formatter("%(asctime)s [%(name)s][None][%(process)d][%(levelname)s]: %(message)s")
            self.fh.setFormatter(self.formatter)
            self.logger.addHandler(self.fh)
            
        except Exception as e:
            print "ERROR: Problems with the log configuration needed by Zabbix-CLI: %s" % e
            sys.exit(1)