25
26
h = logging.FileHandler(filename)
29
30
def setup_logging(home, config, foreground):
30
31
# i hate that stupid logging config format, so just set up logging here.
32
33
log_folder = os.path.join(home, 'logs')
33
34
if not os.path.exists(log_folder):
34
35
os.mkdir(log_folder)
36
37
f = logging.Formatter('%(levelname)-.3s [%(asctime)s.%(msecs)03d] %(name)s: %(message)s',
38
39
debug_log = make_handler(config, os.path.join(log_folder, 'debug.log'))
47
48
access_log = make_handler(config, os.path.join(log_folder, 'access.log'))
48
49
access_log.setLevel(logging.INFO)
49
50
access_log.setFormatter(f)
51
52
logging.getLogger('').addHandler(debug_log)
52
53
logging.getLogger('turbogears.access').addHandler(access_log)
53
54
logging.getLogger('turbogears.controllers').setLevel(logging.INFO)
56
57
logging.getLogger('').addHandler(stdout_log)
66
67
options, args = parser.parse_args()
68
69
parser.error('No filename arguments are used, only options.')
71
72
home = os.path.realpath(os.path.dirname(__file__))
72
73
pidfile = os.path.join(home, 'loggerhead.pid')
75
76
if daemon.is_running(pidfile):
77
78
sys.stderr.write('Did not find loggerhead running in %r; restarting...\n' % (pidfile,))
79
80
# read loggerhead config
81
82
from configobj import ConfigObj
82
83
config = ConfigObj(os.path.join(home, 'loggerhead.conf'), encoding='utf-8')
83
84
extra_path = config.get('bzrpath', None)
85
86
sys.path.insert(0, extra_path)
87
turbogears.update_config(configfile="dev.cfg", modulename="loggerhead.config")
89
potential_overrides = [ ('server.socket_port', int), ('server.webpath', str), ('server.thread_pool', int), ('server.socket_host' ,str) ]
88
#turbogears.update_config(configfile="dev.cfg", modulename="loggerhead.config")
90
potential_overrides = [ ('server.socket_port', int),
91
('server.webpath', str),
92
('server.thread_pool', int),
93
('server.socket_host' ,str) ]
90
94
for key, keytype in potential_overrides:
91
95
value = config.get(key, None)
92
96
if value is not None:
93
97
value = keytype(value)
94
turbogears.config.update({ key: value })
96
if not options.foreground:
97
sys.stderr.write('\n')
98
sys.stderr.write('Launching loggerhead into the background.\n')
99
sys.stderr.write('PID file: %s\n' % (pidfile,))
100
sys.stderr.write('\n')
102
daemon.daemonize(pidfile, home)
98
#turbogears.config.update({ key: value })
100
## if not options.foreground:
101
## sys.stderr.write('\n')
102
## sys.stderr.write('Launching loggerhead into the background.\n')
103
## sys.stderr.write('PID file: %s\n' % (pidfile,))
104
## sys.stderr.write('\n')
106
## daemon.daemonize(pidfile, home)
104
108
setup_logging(home, config, foreground=options.foreground)
106
110
log = logging.getLogger('loggerhead')
107
111
log.info('Starting up...')
109
from loggerhead.controllers import Root
113
# re-index every 6 hours
114
index_freq = config.get('cache_rebuild_frequency', 6 * 3600)
115
turbogears.scheduler.add_interval_task(initialdelay=1, interval=index_freq, action=Root._check_rebuild)
118
turbogears.start_server(Root)
120
log.info('Shutdown.')
116
app = make_middleware(app)
117
app = make_filter(app, None)
119
httpserver.serve(app, host='127.0.0.1', port='9876')
127
122
if __name__ == '__main__':