]> git.ipfire.org Git - location/libloc.git/commitdiff
python: Move common logging code into module
authorMichael Tremer <michael.tremer@ipfire.org>
Tue, 12 May 2020 10:01:59 +0000 (10:01 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Tue, 12 May 2020 10:01:59 +0000 (10:01 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
Makefile.am
src/python/__init__.py
src/python/location-downloader.in
src/python/location-exporter.in
src/python/location-query.in
src/python/logger.py [new file with mode: 0644]

index f07d3e58b75607c20c31895e10d1df5843c10cda..6f4a8c969a1fe873a3f6e0310cbeb176b9ea2c1a 100644 (file)
@@ -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
index d2a4678b3d94cbedae2672e9650bf3555ce15a54..9b570c797c552ecf151e451de2b560980a4b27d3 100644 (file)
@@ -19,3 +19,6 @@
 
 # Import everything from the C module
 from _location import *
+
+# Initialise logging
+from . import logger
index 71a4007eb850e5ba4d67e9cada57faa51f44a67e..ca64a0d62619ec72f1d0191b51c223f68c8b9f3d 100644 (file)
@@ -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):
index cd37db3d4c6fcdbdb2c29f668ecd8977ac8fc83e..0f7fbae3401124f99b9fef9f1de2b18eecf21236 100644 (file)
@@ -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):
index 0384cbc0d0582624e70bf81849d430d0cd8d3c41..260425546aef5752b098804340f4a6066a6f75b8 100644 (file)
@@ -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 (file)
index 0000000..a8f3b59
--- /dev/null
@@ -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 <info@ipfire.org>                #
+#                                                                             #
+# 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)