main: Add the logger name and process ID to the syslog formatter
[package-cache.git] / package_cache / main.py
1 # Copyright (C) 2014 W. Trevor King <wking@tremily.us>
2 #
3 # This file is part of package-cache.
4 #
5 # package-cache is free software: you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by the Free
7 # Software Foundation, either version 3 of the License, or (at your option) any
8 # later version.
9 #
10 # package-cache is distributed in the hope that it will be useful, but WITHOUT
11 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12 # FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
13 # details.
14 #
15 # You should have received a copy of the GNU General Public License along with
16 # package-cache.  If not, see <http://www.gnu.org/licenses/>.
17
18 import argparse as _argparse
19 import logging as _logging
20 import logging.handlers as _logging_handlers
21 import wsgiref.simple_server as _wsgiref_simple_server
22
23 from . import __version__
24 from . import LOG as _MAIN_LOG
25 from . import server as _server
26
27
28 LOG = _logging.getLogger(__name__)
29
30
31 class LoggingRequestHandler (_wsgiref_simple_server.WSGIRequestHandler):
32     def log_message(self, format, *args):
33         """Log a message using LOG
34
35         Use a logger instead of printing directly to stderr.  This
36         method overrides the http.server.BaseHTTPRequestHandler
37         inherited by WSGIRequestHandler.
38         """
39         LOG.info(
40             '%s - - [%s] %s' % (
41                 self.address_string(),
42                 self.log_date_time_string(),
43                 format % args))
44
45
46 def run(*args, **kwargs):
47     """Run the package-cache server using Python's WSGI reference server
48     """
49     parser = _argparse.ArgumentParser(description=__doc__)
50     parser.add_argument(
51         '--version', action='version',
52         version='%(prog)s {}'.format(__version__))
53     parser.add_argument(
54         '--verbose', '-v', action='count',
55         help='Increment the logging verbosity')
56     parser.add_argument(
57         '--syslog', action='store_const', const=True,
58         help='Increment the logging verbosity')
59     parser.add_argument(
60         '--host', metavar='HOSTNAME', default='localhost',
61         help='Host to listen as')
62     parser.add_argument(
63         '--port', metavar='INT', default=4000, type=int,
64         help='Port to listen to')
65     parser.add_argument(
66         '--source', metavar='URL', action='append',
67         help='URL for an upstream mirror')
68     parser.add_argument(
69         '--cache', metavar='PATH', default='/tmp/package-cache',
70         help='Path to the package cache directory')
71
72     args = parser.parse_args()
73
74     if args.verbose:
75         _MAIN_LOG.setLevel(max(
76             _logging.DEBUG,
77             _MAIN_LOG.level - 10 * args.verbose))
78     if args.syslog:
79         while _MAIN_LOG.handlers:
80             h = _MAIN_LOG.handlers[0]
81             _MAIN_LOG.removeHandler(h)
82         handler = _logging_handlers.SysLogHandler(
83             address='/dev/log', facility='daemon')
84         _MAIN_LOG.addHandler(handler)
85         formatter = _logging.Formatter(
86             fmt='%(name)s[%(process)d]: %(message)s')
87         handler.setFormatter(formatter)
88
89     server = _server.Server(sources=args.source or [], cache=args.cache)
90     wsgi = _wsgiref_simple_server.make_server(
91         host=args.host, port=args.port, app=server,
92         handler_class=LoggingRequestHandler)
93     LOG.info('serving WSGI on {}:{}'.format(args.host, args.port))
94     wsgi.serve_forever()