deb-ryu/ryu/log.py
YAMADA Hideki c5c7dc9cfe ryu/log: bugfix
$ ryu-manager --log_file $HOME/ryu.log
Traceback (most recent call last):
(...)
  File "/usr/local/lib/python2.7/dist-packages/ryu-1.1-py2.7.egg/ryu/log.py", line 74, in init_log
    logging.addHandler(logging.handlers.WatchedFileHandler(log_file))
AttributeError: 'module' object has no attribute 'addHandler'

$ ryu-manager --log_file $HOME/ryu.log
Traceback (most recent call last):
(...)
  File "/usr/local/lib/python2.7/dist-packages/ryu-1.4-py2.7.egg/ryu/log.py", line 74, in init_log
    log.addHandler(logging.handlers.WatchedFileHandler(log_file))
AttributeError: 'module' object has no attribute 'handlers'

$ ryu-manager --log_file $HOME/ryu.log
Traceback (most recent call last):
(...)
  File "/usr/local/lib/python2.7/dist-packages/ryu-1.4-py2.7.egg/ryu/log.py", line 76, in init_log
    mode = int(FLAGS.log_file_mnode, 8)
  File "/usr/local/lib/python2.7/dist-packages/gflags.py", line 810, in __getattr__
    raise AttributeError(name)
AttributeError: log_file_mnode

$ ryu-manager --log_dir $HOME/log/
Traceback (most recent call last):
(...)
  File "/usr/local/lib/python2.7/dist-packages/ryu-1.4-py2.7.egg/ryu/log.py", line 54, in _get_log_file
    return os.path.join(FLAGS.logdir,
  File "/usr/local/lib/python2.7/dist-packages/gflags.py", line 810, in __getattr__
    raise AttributeError(name)
AttributeError: logdir

Signed-off-by: YAMADA Hideki <yamada.hideki@po.ntts.co.jp>
Signed-off-by: FUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp>
2012-11-28 08:02:15 +09:00

85 lines
2.5 KiB
Python

# Copyright (C) 2011 Nippon Telegraph and Telephone Corporation.
# Copyright (C) 2011 Isaku Yamahata <yamahata at valinux co jp>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
# implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import gflags
import inspect
import logging
import logging.handlers
import os
import sys
FLAGS = gflags.FLAGS
gflags.DEFINE_integer('default_log_level', None, 'default log level')
gflags.DEFINE_bool('verbose', False, 'show debug output')
gflags.DEFINE_bool('use_stderr', True, 'log to standard error')
gflags.DEFINE_string('use_syslog', False, 'output to syslog')
gflags.DEFINE_string('log_dir', None, 'log file directory')
gflags.DEFINE_string('log_file', None, 'log file name')
gflags.DEFINE_string('log_file_mode', '0644', 'default log file permission')
_EARLY_LOG_HANDLER = None
def early_init_log(level=None):
global _EARLY_LOG_HANDLER
_EARLY_LOG_HANDLER = logging.StreamHandler(sys.stderr)
log = logging.getLogger()
log.addHandler(_EARLY_LOG_HANDLER)
if level is not None:
log.setLevel(level)
def _get_log_file():
if FLAGS.log_file:
return FLAGS.log_file
if FLAGS.log_dir:
return os.path.join(FLAGS.log_dir,
os.path.basename(inspect.stack()[-1][1])) + '.log'
return None
def init_log():
global _EARLY_LOG_HANDLER
log = logging.getLogger()
if FLAGS.use_stderr:
log.addHandler(logging.StreamHandler(sys.stderr))
if _EARLY_LOG_HANDLER is not None:
log.removeHandler(_EARLY_LOG_HANDLER)
_EARLY_LOG_HANDLER = None
if FLAGS.use_syslog:
syslog = logging.handlers.SysLogHandler(address='/dev/log')
log.addHandler(syslog)
log_file = _get_log_file()
if log_file is not None:
log.addHandler(logging.handlers.WatchedFileHandler(log_file))
mode = int(FLAGS.log_file_mode, 8)
os.chmod(log_file, mode)
if FLAGS.verbose:
log.setLevel(logging.DEBUG)
elif FLAGS.default_log_level is not None:
log.setLevel(FLAGS.default_log_level)
else:
log.setLevel(logging.INFO)