From e44b30f4e3409c0c68fd82e8b9618e5fc4b3e0e9 Mon Sep 17 00:00:00 2001 From: Michael Tremer Date: Tue, 12 May 2020 10:01:59 +0000 Subject: [PATCH] python: Move common logging code into module Signed-off-by: Michael Tremer --- Makefile.am | 3 ++- src/python/__init__.py | 3 +++ src/python/location-downloader.in | 25 ++----------------- src/python/location-exporter.in | 25 ++----------------- src/python/location-query.in | 1 - src/python/logger.py | 40 +++++++++++++++++++++++++++++++ 6 files changed, 49 insertions(+), 48 deletions(-) create mode 100644 src/python/logger.py diff --git a/Makefile.am b/Makefile.am index f07d3e5..6f4a8c9 100644 --- a/Makefile.am +++ b/Makefile.am @@ -153,7 +153,8 @@ CLEANFILES += \ src/libloc.pc dist_pkgpython_PYTHON = \ - src/python/__init__.py + src/python/__init__.py \ + src/python/logger.py pyexec_LTLIBRARIES = \ src/python/_location.la diff --git a/src/python/__init__.py b/src/python/__init__.py index d2a4678..9b570c7 100644 --- a/src/python/__init__.py +++ b/src/python/__init__.py @@ -19,3 +19,6 @@ # Import everything from the C module from _location import * + +# Initialise logging +from . import logger diff --git a/src/python/location-downloader.in b/src/python/location-downloader.in index 71a4007..ca64a0d 100644 --- a/src/python/location-downloader.in +++ b/src/python/location-downloader.in @@ -21,7 +21,6 @@ import argparse import datetime import gettext import logging -import logging.handlers import lzma import os import random @@ -42,29 +41,9 @@ MIRRORS = ( "https://people.ipfire.org/~ms/location/", ) -def setup_logging(level=logging.INFO): - l = logging.getLogger("location-downloader") - l.setLevel(level) - - # Log to console - h = logging.StreamHandler() - h.setLevel(logging.DEBUG) - l.addHandler(h) - - # Log to syslog - h = logging.handlers.SysLogHandler(address="/dev/log", - facility=logging.handlers.SysLogHandler.LOG_DAEMON) - h.setLevel(logging.INFO) - l.addHandler(h) - - # Format syslog messages - formatter = logging.Formatter("location-downloader[%(process)d]: %(message)s") - h.setFormatter(formatter) - - return l - # Initialise logging -log = setup_logging() +log = logging.getLogger("location.downloader") +log.propagate = 1 # i18n def _(singular, plural=None, n=None): diff --git a/src/python/location-exporter.in b/src/python/location-exporter.in index cd37db3..0f7fbae 100644 --- a/src/python/location-exporter.in +++ b/src/python/location-exporter.in @@ -22,7 +22,6 @@ import gettext import io import ipaddress import logging -import logging.handlers import os.path import socket import sys @@ -30,29 +29,9 @@ import sys # Load our location module import location -def setup_logging(level=logging.INFO): - l = logging.getLogger("location-exporter") - l.setLevel(level) - - # Log to console - h = logging.StreamHandler() - h.setLevel(logging.DEBUG) - l.addHandler(h) - - # Log to syslog - h = logging.handlers.SysLogHandler(address="/dev/log", - facility=logging.handlers.SysLogHandler.LOG_DAEMON) - h.setLevel(logging.INFO) - l.addHandler(h) - - # Format syslog messages - formatter = logging.Formatter("location-exporter[%(process)d]: %(message)s") - h.setFormatter(formatter) - - return l - # Initialise logging -log = setup_logging() +log = logging.getLogger("location.exporter") +log.propagate = 1 # i18n def _(singular, plural=None, n=None): diff --git a/src/python/location-query.in b/src/python/location-query.in index 0384cbc..2604255 100644 --- a/src/python/location-query.in +++ b/src/python/location-query.in @@ -23,7 +23,6 @@ import ipaddress import os import socket import sys -import syslog # Load our location module import location diff --git a/src/python/logger.py b/src/python/logger.py new file mode 100644 index 0000000..a8f3b59 --- /dev/null +++ b/src/python/logger.py @@ -0,0 +1,40 @@ +#!/usr/bin/python3 +############################################################################### +# # +# libloc - A library to determine the location of someone on the Internet # +# # +# Copyright (C) 2020 IPFire Development Team # +# # +# This library is free software; you can redistribute it and/or # +# modify it under the terms of the GNU Lesser General Public # +# License as published by the Free Software Foundation; either # +# version 2.1 of the License, or (at your option) any later version. # +# # +# This library 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 # +# Lesser General Public License for more details. # +# # +############################################################################### + +import logging +import logging.handlers + +# Initialise root logger +log = logging.getLogger("location") +log.setLevel(logging.INFO) + +# Log to console +handler = logging.StreamHandler() +handler.setLevel(logging.DEBUG) +log.addHandler(handler) + +# Log to syslog +handler = logging.handlers.SysLogHandler(address="/dev/log", + facility=logging.handlers.SysLogHandler.LOG_DAEMON) +handler.setLevel(logging.INFO) +log.addHandler(handler) + +# Format syslog messages +formatter = logging.Formatter("%(message)s") +handler.setFormatter(formatter) -- 2.39.2