~loggerhead-team/loggerhead/trunk-rich

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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#!/usr/bin/env python
# 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 of the License, 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

"""A script for starting the loggerhead process."""


import logging
import logging.handlers
from optparse import OptionParser
import os
import sys
import urlparse

from configobj import ConfigObj

from paste import httpserver
from paste.httpexceptions import make_middleware
from paste.translogger import make_filter

from loggerhead import daemon, release
from loggerhead.apps.config import Root


def make_handler(config, filename):
    roll = config.get('log.roll', 'never')
    if roll == 'daily':
        h = logging.handlers.TimedRotatingFileHandler(filename, 'midnight', 0, 100)
    elif roll == 'weekly':
        h = logging.handlers.TimedRotatingFileHandler(filename, 'W0', 0, 100)
    else:
        h = logging.FileHandler(filename)
    return h


def setup_logging(home, config, foreground):
    # i hate that stupid logging config format, so just set up logging here.

    log_folder = os.path.join(home, 'logs')
    if not os.path.exists(log_folder):
        os.mkdir(log_folder)

    f = logging.Formatter('%(levelname)-.3s [%(asctime)s.%(msecs)03d] %(name)s: %(message)s',
                          '%Y%m%d-%H:%M:%S')
    debug_log = make_handler(config, os.path.join(log_folder, 'debug.log'))
    debug_log.setLevel(logging.DEBUG)
    debug_log.setFormatter(f)
    if foreground:
        stdout_log = logging.StreamHandler(sys.stdout)
        stdout_log.setLevel(logging.DEBUG)
        stdout_log.setFormatter(f)
    f = logging.Formatter('[%(asctime)s.%(msecs)03d] %(message)s',
                          '%Y%m%d-%H:%M:%S')
    access_log = make_handler(config, os.path.join(log_folder, 'access.log'))
    access_log.setLevel(logging.INFO)
    access_log.setFormatter(f)

    logging.getLogger('').setLevel(logging.DEBUG)
    logging.getLogger('').addHandler(debug_log)
    logging.getLogger('wsgi').addHandler(access_log)

    if foreground:
        logging.getLogger('').addHandler(stdout_log)



def main():
    home = os.path.realpath(os.path.dirname(__file__))
    default_pidfile = os.path.join(home, 'loggerhead.pid')
    parser = OptionParser(usage='usage: %prog [options]', version='%prog ' + release.version)
    parser.add_option('-f', '--foreground', action='store_true', dest='foreground', default=False,
                      help="run in the foreground; don't daemonize")
    parser.add_option('-C', '--check', action='store_true', dest='check', default=False,
                      help="only start if not already running (useful for cron jobs)")
    parser.add_option('-p', '--pidfile', dest="pidfile", default=default_pidfile,
                      help="override pidfile location")
    options, args = parser.parse_args()
    if len(args) > 0:
        parser.error('No filename arguments are used, only options.')

    if options.check:
        if daemon.is_running(options.pidfile):
            sys.exit(0)
        sys.stderr.write('Did not find loggerhead running in %r; restarting...\n' % (options.pidfile,))

    # read loggerhead config

    config = ConfigObj(os.path.join(home, 'loggerhead.conf'), encoding='utf-8')
    extra_path = config.get('bzrpath', None)
    if extra_path:
        sys.path.insert(0, extra_path)

    #turbogears.update_config(configfile="dev.cfg", modulename="loggerhead.config")

    potential_overrides = [ ('server.socket_port', int),
                            ('server.webpath', str),
                            ('server.thread_pool', int),
                            ('server.socket_host' ,str) ]
    server_port = int(config.get('server.socket_port', 8080))
    nworkers = int(config.get('server.thread_pool', 10))
    server_host = config.get('server.socket_host', '0.0.0.0')
    webpath = config.get('server.webpath', None)

    for key, keytype in potential_overrides:
        value = config.get(key, None)
        if value is not None:
            value = keytype(value)
            #turbogears.config.update({ key: value })

    if not options.foreground:
        sys.stderr.write('\n')
        sys.stderr.write('Launching loggerhead into the background.\n')
        sys.stderr.write('PID file: %s\n' % (options.pidfile,))
        sys.stderr.write('\n')

        daemon.daemonize(options.pidfile, home)

    setup_logging(home, config, foreground=options.foreground)

    log = logging.getLogger('loggerhead')
    log.info('Starting up...')

    app = Root(config)

    app = app
    app = make_middleware(app)
    app = make_filter(app, None)

    if webpath:
        scheme, netloc, path, blah, blah, blah = urlparse.urlparse(webpath)
        def app(environ, start_response, orig=app):
            environ['SCRIPT_NAME'] = path
            environ['HTTP_HOST'] = netloc
            return orig(environ, start_response)

    try:
        httpserver.serve(
            app, host=server_host, port=server_port,
            threadpool_workers=nworkers)
    finally:
        log.info('Shutdown.')
        try:
            os.remove(options.pidfile)
        except OSError:
            pass


if __name__ == '__main__':
    main()