Fix #531 set default log level to ERROR and add 2 command-line options,

"-L, --loglevel" and "-q, --quiet".
This commit is contained in:
Andrew Resch 2008-11-28 23:28:06 +00:00
parent e759d61266
commit 1440d6b247
4 changed files with 73 additions and 18 deletions

View File

@ -33,6 +33,13 @@ The UI that you wish to launch, current options include: gtk, web or null
.TP .TP
.I -l LOGFILE, --logfile=LOGFILE .I -l LOGFILE, --logfile=LOGFILE
Output to designated logfile instead of stdout Output to designated logfile instead of stdout
.TP
.I -L LOGLEVEL, --loglevel=LOGLEVEL
Set the log level (default is error): none, info, warning, error, critical, debug
.TP
.I -q --quiet
Sets the log level to 'none', this is the same as `-L none`
.SH SEE ALSO .SH SEE ALSO
.B Homepage: .B Homepage:

View File

@ -36,6 +36,12 @@ Output to designated logfile instead of stdout
.TP .TP
.I -P PIDFILE, --pidfile=PIDFILE .I -P PIDFILE, --pidfile=PIDFILE
Use pidfile to store process id Use pidfile to store process id
.TP
.I -L LOGLEVEL, --loglevel=LOGLEVEL
Set the log level (default is error): none, info, warning, error, critical, debug
.TP
.I -q --quiet
Sets the log level to 'none', this is the same as `-L none`
.SH SEE ALSO .SH SEE ALSO
.B Homepage: .B Homepage:

View File

@ -29,10 +29,31 @@ import logging
# Setup the logger # Setup the logger
logging.basicConfig( logging.basicConfig(
level=logging.DEBUG, level=logging.ERROR,
format="[%(levelname)-8s] %(asctime)s %(module)s:%(lineno)d %(message)s", format="[%(levelname)-8s] %(asctime)s %(module)s:%(lineno)d %(message)s",
datefmt="%H:%M:%S" datefmt="%H:%M:%S"
) )
def setLoggerLevel(level):
"""
Sets the logger level.
:param level: str, a string representing the desired level
"""
levels = {
"info": logging.INFO,
"warning": logging.WARNING,
"error": logging.ERROR,
"none": logging.CRITICAL,
"debug": logging.DEBUG
}
if level not in levels:
return
global LOG
LOG.setLevel(levels[level])
# Get the logger # Get the logger
LOG = logging.getLogger("deluge") LOG = logging.getLogger("deluge")

View File

@ -37,6 +37,8 @@ import deluge.common
def start_ui(): def start_ui():
"""Entry point for ui script""" """Entry point for ui script"""
import deluge.common
# Setup the argument parser # Setup the argument parser
parser = OptionParser(usage="%prog [options] [actions]", parser = OptionParser(usage="%prog [options] [actions]",
version=deluge.common.get_version()) version=deluge.common.get_version())
@ -49,10 +51,17 @@ def start_ui():
help="Output to designated logfile instead of stdout", action="store", type="str") help="Output to designated logfile instead of stdout", action="store", type="str")
parser.add_option("-a", "--args", dest="args", parser.add_option("-a", "--args", dest="args",
help="Arguments to pass to UI, -a '--option args'", action="store", type="str") help="Arguments to pass to UI, -a '--option args'", action="store", type="str")
parser.add_option("-L", "--loglevel", dest="loglevel",
help="Set the log level: none, info, warning, error, critical, debug", action="store", type="str")
parser.add_option("-q", "--quiet", dest="quiet",
help="Sets the log level to 'none', this is the same as `-L none`", action="store_true", default=False)
# Get the options and args from the OptionParser # Get the options and args from the OptionParser
(options, args) = parser.parse_args() (options, args) = parser.parse_args()
if options.quiet:
options.loglevel = "none"
if options.config: if options.config:
if not os.path.exists(options.config): if not os.path.exists(options.config):
# Try to create the config folder if it doesn't exist # Try to create the config folder if it doesn't exist
@ -79,6 +88,10 @@ def start_ui():
sys.stdin = None sys.stdin = None
from deluge.log import LOG as log from deluge.log import LOG as log
# Set the log level if necessary
if options.loglevel:
import deluge.log
deluge.log.setLoggerLevel(options.loglevel)
version = deluge.common.get_version() version = deluge.common.get_version()
if deluge.common.get_revision() != "": if deluge.common.get_revision() != "":
@ -110,10 +123,17 @@ def start_daemon():
help="Set the logfile location", action="store", type="str") help="Set the logfile location", action="store", type="str")
parser.add_option("-P", "--pidfile", dest="pidfile", parser.add_option("-P", "--pidfile", dest="pidfile",
help="Use pidfile to store process id", action="store", type="str") help="Use pidfile to store process id", action="store", type="str")
parser.add_option("-L", "--loglevel", dest="loglevel",
help="Set the log level: none, info, warning, error, critical, debug", action="store", type="str")
parser.add_option("-q", "--quiet", dest="quiet",
help="Sets the log level to 'none', this is the same as `-L none`", action="store_true", default=False)
# Get the options and args from the OptionParser # Get the options and args from the OptionParser
(options, args) = parser.parse_args() (options, args) = parser.parse_args()
if options.quiet:
options.loglevel = "none"
if options.config: if options.config:
if not os.path.exists(options.config): if not os.path.exists(options.config):
# Try to create the config folder if it doesn't exist # Try to create the config folder if it doesn't exist
@ -125,16 +145,23 @@ def start_daemon():
if not os.path.exists(deluge.common.get_default_config_dir()): if not os.path.exists(deluge.common.get_default_config_dir()):
os.makedirs(deluge.common.get_default_config_dir()) os.makedirs(deluge.common.get_default_config_dir())
# Returns a path to the logfile # Opens a log file and redirects stdout to it
def open_logfile(): def open_logfile():
path = None
if options.logfile: if options.logfile:
return options.logfile path = options.logfile
else: else:
if options.config: if options.config:
return os.path.join(options.config, "deluged.log") path = os.path.join(options.config, "deluged.log")
else: else:
config_dir = deluge.common.get_default_config_dir() config_dir = deluge.common.get_default_config_dir()
return os.path.join(config_dir, "deluged.log") path = os.path.join(config_dir, "deluged.log")
# Open a logfile
if path:
sys.stdout = open(path, "wb")
sys.stderr = sys.stdout
sys.stdin = None
# Writes out a pidfile if necessary # Writes out a pidfile if necessary
def write_pidfile(): def write_pidfile():
@ -145,24 +172,13 @@ def start_daemon():
if not options.donot: if not options.donot:
# Windows check, we log to the config folder by default # Windows check, we log to the config folder by default
if deluge.common.windows_check(): if deluge.common.windows_check():
# Open a logfile open_logfile()
sys.stdout = open(open_logfile(), "wb")
sys.stderr = sys.stdout
sys.stdin = None
# Write pidfile
write_pidfile() write_pidfile()
else: else:
if os.fork() == 0: if os.fork() == 0:
os.setsid() os.setsid()
if os.fork() == 0: if os.fork() == 0:
# Open a logfile open_logfile()
sys.stdout = open(open_logfile(), "wb")
sys.stderr = sys.stdout
sys.stdin = None
# Write pidfile
write_pidfile() write_pidfile()
else: else:
os._exit(0) os._exit(0)
@ -172,5 +188,10 @@ def start_daemon():
# Do not daemonize # Do not daemonize
write_pidfile() write_pidfile()
# Set the log level if necessary
if options.loglevel:
import deluge.log
deluge.log.setLoggerLevel(options.loglevel)
from deluge.core.daemon import Daemon from deluge.core.daemon import Daemon
Daemon(options, args) Daemon(options, args)