File: log.py

package info (click to toggle)
prelude-correlator 4.1.1-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 1,056 kB
  • sloc: python: 1,686; sh: 34; makefile: 23
file content (99 lines) | stat: -rw-r--r-- 2,903 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# Copyright (C) 2009-2017 CS-SI. All Rights Reserved.
# Author: Yoann Vandoorselaere <yoann.v@prelude-ids.com>
#
# This file is part of the Prelude-Correlator program.
#
# This program 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 2, or (at your option)
# any later version.
#
# This program 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 this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

import prelude
import logging
import logging.config
import logging.handlers
import sys
import os
import stat


debug_level = 0

def _debug(self, msg, *args, **kwargs):
    level = kwargs.pop("level", 0)

    if debug_level and level <= debug_level:
        self.log(logging.DEBUG, msg, *args, **kwargs)


logging.Logger.debug = _debug


def __C_log_callback(level, log):
    log = log.rstrip('\n')
    logger = getLogger("libprelude")

    if level == prelude.PreludeLog.DEBUG:
        logger.debug(log)

    elif level == prelude.PreludeLog.INFO:
        logger.info(log)

    elif level == prelude.PreludeLog.WARNING:
        logger.warning(log)

    elif level == prelude.PreludeLog.ERROR:
        logger.error(log)

    elif level == prelude.PreludeLog.CRITICAL:
        logger.critical(log)

    else:
        logger.warning(("[unknown:%d] " % level) + log)


def getSyslogHandlerAddress():
    for f in ("/dev/log", "/var/run/log", "/var/run/syslog"):
        try:
            if stat.S_ISSOCK(os.stat(f).st_mode):
                return f
        except:
            pass

    return ("localhost", 514)

def initLogger(options):
    global debug_level

    debug_level = options.debug

    try:
        prelude.PreludeLog.setCallback(__C_log_callback)
    except:
        # PreludeLog is available in recent libprelude version, we do not want to fail if it's not.
        pass

    try:
        logging.config.fileConfig(options.config)
    except Exception as e:
        DATEFMT = "%d %b %H:%M:%S"
        FORMAT="%(asctime)s %(name)s (pid:%(process)d) %(levelname)s: %(message)s"
        logging.basicConfig(level=logging.DEBUG, format=FORMAT, datefmt=DATEFMT, stream=sys.stderr)

    if options.daemon is True:
        hdlr = logging.handlers.SysLogHandler(getSyslogHandlerAddress(), facility=logging.handlers.SysLogHandler.LOG_DAEMON)
        hdlr.setFormatter(logging.Formatter('%(name)s: %(levelname)s: %(message)s'))
        logging.getLogger().addHandler(hdlr)


def getLogger(name=__name__):
    return logging.getLogger(name)