diff options
author | Sebastian Deiss <sebastian.deiss@atos.net> | 2017-09-14 13:59:43 +0200 |
---|---|---|
committer | Sebastian Deiss <sebastian.deiss@atos.net> | 2017-09-14 13:59:43 +0200 |
commit | 213291648ee64d91f34005727bcdb940a500852e (patch) | |
tree | 6f23c7cedd864263a3e51693e3ab6a7be457eb83 | |
parent | 6ff0a2ce93b626300a18f7efeb6974b41beb065a (diff) |
Change logging format and make it configurable
-rw-r--r-- | peekaboo.conf.sample | 18 | ||||
-rw-r--r-- | peekaboo/__init__.py | 2 | ||||
-rw-r--r-- | peekaboo/config.py | 29 | ||||
-rw-r--r-- | peekaboo/cuckoo_wrapper.py | 6 | ||||
-rw-r--r-- | peekaboo/daemon.py | 5 | ||||
-rw-r--r-- | peekaboo/db.py | 3 | ||||
-rw-r--r-- | peekaboo/debug.py | 5 | ||||
-rw-r--r-- | peekaboo/pjobs.py | 5 | ||||
-rw-r--r-- | peekaboo/ruleset/processor.py | 5 | ||||
-rw-r--r-- | peekaboo/ruleset/rules.py | 5 | ||||
-rw-r--r-- | peekaboo/sample.py | 6 | ||||
-rw-r--r-- | peekaboo/toolbox/plugins/oneanalysis.py | 5 |
12 files changed, 70 insertions, 24 deletions
diff --git a/peekaboo.conf.sample b/peekaboo.conf.sample index 25aecc4..c9679eb 100644 --- a/peekaboo.conf.sample +++ b/peekaboo.conf.sample @@ -11,9 +11,6 @@ socket_file : /opt/peekaboo/peekaboo.sock pid_file : /opt/peekaboo/peekaboo.pid interpreter : /usr/bin/python chown2me_exec : /opt/peekaboo/bin/chown2me -# log_level -# possible values: CRITICAL | ERROR | WARNING | INFO | DEBUG -log_level : INFO worker_count : 3 sample_base_dir : /tmp job_hash_regex : /var/lib/amavis/tmp/([^/]+)/parts.* @@ -21,6 +18,20 @@ job_hash_regex : /var/lib/amavis/tmp/([^/]+)/parts.* # additional code execution at runtime. use_debug_module : no + +# +# Logging configuration +# +[logging] +# log_level +# possible values: CRITICAL | ERROR | WARNING | INFO | DEBUG +log_level : DEBUG +# note that any % must be escaped with another %. +log_format : %%(asctime)s - %%(name)s - (%%(threadName)s) - %%(levelname)s - %%(message)s +# if you use systemd you don't want the timestamp +# log_format : %%(name)s - (%%(threadName)s) - %%(levelname)s - %%(message)s + + # # Database configuration # @@ -32,6 +43,7 @@ url : sqlite:////path/to/database.db # PostgreSQL # url : postgresql://user:password@host:port/database + # # Cuckoo specific settings # diff --git a/peekaboo/__init__.py b/peekaboo/__init__.py index 8bceed8..9e49afb 100644 --- a/peekaboo/__init__.py +++ b/peekaboo/__init__.py @@ -65,8 +65,6 @@ Peekaboo Extended Email Attachment Behavior Observation Owl (never mind the K) """.format(__version__) -logger = logging.getLogger('Peekaboo') - # # Helpers diff --git a/peekaboo/config.py b/peekaboo/config.py index 66544bb..cc96840 100644 --- a/peekaboo/config.py +++ b/peekaboo/config.py @@ -26,7 +26,9 @@ import sys import logging from ConfigParser import SafeConfigParser, NoSectionError, NoOptionError -from peekaboo import logger + + +logger = logging.getLogger(__name__) class PeekabooConfig(object): @@ -42,6 +44,8 @@ class PeekabooConfig(object): self.pid_file = None self.sock_file = None self.log_level = logging.INFO + self.log_format = '%(asctime)s - %(name)s - (%(threadName)s) - ' \ + '%(levelname)s - %(message)s' self.interpreter = None self.chown2me_exec = None self.worker_count = 3 @@ -64,8 +68,9 @@ class PeekabooConfig(object): config.read(config_file) self.__config = config try: - log_level = config.get('global', 'log_level') + log_level = config.get('logging', 'log_level') self.log_level = self.__parse_log_level(log_level) + self.log_format = config.get('logging', 'log_format') self.user = config.get('global', 'user') self.group = config.get('global', 'group') self.pid_file = config.get('global', 'pid_file') @@ -81,7 +86,8 @@ class PeekabooConfig(object): self.cuckoo_storage = config.get('cuckoo', 'storage_path') self.cuckoo_exec = config.get('cuckoo', 'exec') self.cuckoo_submit = config.get('cuckoo', 'submit').split(' ') - logger.setLevel(self.log_level) + # Update logging with what we just parsed from the config + self.__setup_logging() except NoSectionError as e: logger.critical('configuration section not found') logger.exception(e) @@ -125,17 +131,20 @@ class PeekabooConfig(object): """ Setup logging to console. """ - logger.setLevel(self.log_level) + _logger = logging.getLogger() + + # Check if we already have a log handler + if len(_logger.handlers) > 0: + # Remove all handlers + for handler in _logger.handlers: + _logger.removeHandler(handler) # log format - log_formatter = logging.Formatter('%(asctime)s - %(levelname)s - ' - '%(module)s - %(threadName)s - ' - '%(process)s - %(message)s') + log_formatter = logging.Formatter(self.log_format) # create console handler and set level to debug to_console_log_handler = logging.StreamHandler(sys.stdout) - to_console_log_handler.setLevel(logging.DEBUG) to_console_log_handler.setFormatter(log_formatter) - logger.addHandler(to_console_log_handler) - logger.setLevel(self.log_level) + _logger.addHandler(to_console_log_handler) + _logger.setLevel(self.log_level) def __str__(self): sections = {} diff --git a/peekaboo/cuckoo_wrapper.py b/peekaboo/cuckoo_wrapper.py index 121fa65..4507373 100644 --- a/peekaboo/cuckoo_wrapper.py +++ b/peekaboo/cuckoo_wrapper.py @@ -25,11 +25,15 @@ import re import os +import logging from twisted.internet import protocol -from peekaboo import logger, MultiRegexMatcher +from peekaboo import MultiRegexMatcher import peekaboo.pjobs as pjobs +logger = logging.getLogger(__name__) + + class CuckooManager(protocol.ProcessProtocol): """ Class that is used by twisted.internet.reactor to process Cuckoo diff --git a/peekaboo/daemon.py b/peekaboo/daemon.py index 9e2d684..e04df45 100644 --- a/peekaboo/daemon.py +++ b/peekaboo/daemon.py @@ -34,7 +34,7 @@ import logging from argparse import ArgumentParser from sdnotify import SystemdNotifier from twisted.internet import reactor -from peekaboo import _owl, __version__, logger +from peekaboo import _owl, __version__ from peekaboo.config import PeekabooConfig from peekaboo.db import PeekabooDBHandler from peekaboo.cuckoo_wrapper import CuckooManager @@ -42,6 +42,9 @@ import peekaboo.pjobs as pjobs import peekaboo.sample as sample +logger = logging.getLogger(__name__) + + class PeekabooStreamServer(SocketServer.ThreadingUnixStreamServer): """ Asynchronous server. diff --git a/peekaboo/db.py b/peekaboo/db.py index 0807e91..788804d 100644 --- a/peekaboo/db.py +++ b/peekaboo/db.py @@ -30,11 +30,12 @@ from sqlalchemy.ext.declarative import declarative_base from sqlalchemy.engine import create_engine from sqlalchemy.orm import sessionmaker from sqlalchemy.orm import scoped_session -from peekaboo import logger from peekaboo.ruleset import Result, RuleResult import threading +import logging +logger = logging.getLogger(__name__) Base = declarative_base() diff --git a/peekaboo/debug.py b/peekaboo/debug.py index 084e1d5..936e66a 100644 --- a/peekaboo/debug.py +++ b/peekaboo/debug.py @@ -44,7 +44,10 @@ import os import fileinput import sys import threading -from peekaboo import logger +import logging + + +logger = logging.getLogger(__name__) def debug(): diff --git a/peekaboo/pjobs.py b/peekaboo/pjobs.py index 85e8ee5..3b3c118 100644 --- a/peekaboo/pjobs.py +++ b/peekaboo/pjobs.py @@ -24,13 +24,16 @@ import threading +import logging from Queue import Queue -from peekaboo import logger from peekaboo.ruleset import Result from peekaboo.ruleset.processor import evaluate from peekaboo.exceptions import CuckooReportPendingException +logger = logging.getLogger(__name__) + + class Jobs(object): """ Data structure to share connection->queue information between threads diff --git a/peekaboo/ruleset/processor.py b/peekaboo/ruleset/processor.py index ce69b45..94cc7df 100644 --- a/peekaboo/ruleset/processor.py +++ b/peekaboo/ruleset/processor.py @@ -24,8 +24,8 @@ import os +import logging from shutil import copyfile -from peekaboo import logger from peekaboo.ruleset import Result, RuleResult from peekaboo.ruleset.rules import * from peekaboo.exceptions import CuckooReportPendingException @@ -40,6 +40,9 @@ from peekaboo.toolbox.plugins.oneanalysis import OneAnalysis ''' +logger = logging.getLogger(__name__) + + def evaluate(sample): """ function that is run by a worker for every Sample object. diff --git a/peekaboo/ruleset/rules.py b/peekaboo/ruleset/rules.py index 611bc52..d4ff2bc 100644 --- a/peekaboo/ruleset/rules.py +++ b/peekaboo/ruleset/rules.py @@ -25,10 +25,13 @@ import traceback import re -from peekaboo import logger +import logging from peekaboo.ruleset import Result, RuleResult +logger = logging.getLogger(__name__) + + def known(s): tb = traceback.extract_stack() tb = tb[-1] diff --git a/peekaboo/sample.py b/peekaboo/sample.py index a1b927c..c6bb8c7 100644 --- a/peekaboo/sample.py +++ b/peekaboo/sample.py @@ -34,16 +34,20 @@ import json import errno import string import shutil +import logging from ConfigParser import SafeConfigParser from random import choice from datetime import datetime from oletools.olevba import VBA_Parser -from peekaboo import logger, MultiRegexMatcher +from peekaboo import MultiRegexMatcher from peekaboo.exceptions import CuckooReportPendingException import peekaboo.pjobs as pjobs import peekaboo.ruleset as ruleset +logger = logging.getLogger(__name__) + + class SampleMetaInfo(object): """ DumpInfo data structure that contains additional metadata about diff --git a/peekaboo/toolbox/plugins/oneanalysis.py b/peekaboo/toolbox/plugins/oneanalysis.py index d03defb..18fed4b 100644 --- a/peekaboo/toolbox/plugins/oneanalysis.py +++ b/peekaboo/toolbox/plugins/oneanalysis.py @@ -26,12 +26,15 @@ import threading import traceback import sys +import logging import peekaboo.pjobs -from peekaboo import logger from peekaboo.ruleset import RuleResult from peekaboo.exceptions import CuckooReportPendingException +logger = logging.getLogger(__name__) + + def singleton(class_): instances = {} |