Skip to content
Browse files

Add --log-file option.

Since we plan to distribute this as an egg, we can't store
a log configuration in the same dir as mc3p.util because we
might not have permissions.

With this commit, we use a basicConfig at ERROR level by default.
If the user specifies the --log-file option, we use the specified
logfile (and create it if it doesn't exist).
  • Loading branch information...
1 parent efaf8fb commit 3ac669a9772a464ce87c3a44010d81190367cfc0 @mmcgill committed Oct 24, 2011
Showing with 11 additions and 7 deletions.
  1. +5 −2 mc3p/
  2. +6 −5 mc3p/
7 mc3p/
@@ -45,6 +45,8 @@ def parse_args():
parser.add_option("-l", "--log-level", dest="loglvl", metavar="LEVEL",
help="Override logging.conf root log level")
+ parser.add_option("--log-file", dest='logfile', metavar="FILE", default=None,
+ help="logging configuration file (optional)")
parser.add_option("-p", "--local-port", dest="locport", metavar="PORT", default="34343",
type="int", help="Listen on this port")
parser.add_option("--plugin", dest="plugins", metavar="ID:PLUGIN(ARGS)", type="string",
@@ -238,10 +240,11 @@ def parse_packet(stream, msg_spec, side):
if __name__ == "__main__":
- logging.basicConfig(level=logging.INFO)
+ logging.basicConfig(level=logging.ERROR)
(host, port, opts, pcfg) = parse_args()
- util.config_logging()
+ if opts.logfile:
+ util.config_logging(opts.logfile)
if opts.loglvl:
logging.root.setLevel(getattr(logging, opts.loglvl.upper()))
11 mc3p/
@@ -110,14 +110,15 @@ def write_default_logging_file(lpath):
logging_configured = False
-def config_logging():
+def config_logging(logfile=None):
"""Configure logging. Can safely be called multiple times."""
global logging_configured
if not logging_configured:
dir = os.path.dirname(os.path.abspath(__file__))
- lpath = os.path.join(dir, 'logging.conf')
- if not os.path.exists(lpath):
- write_default_logging_file(lpath)
- logging.config.fileConfig(lpath)
+ if not logfile:
+ logfile = os.path.join(dir, 'logging.conf')
+ if not os.path.exists(logfile):
+ write_default_logging_file(logfile)
+ logging.config.fileConfig(logfile)
logging_configured = True

0 comments on commit 3ac669a

Please sign in to comment.
Something went wrong with that request. Please try again.