Commits

Andrew Peterson  committed ddec70c

Moved reusable logger initialization code to the djlib.

  • Participants
  • Parent commits 1384c35

Comments (0)

Files changed (2)

File djlib/logger.py

 CALL = 5 # below DEBUG(10)
 addLevelName(CALL, "CALL")
 
+def add_log_args(arg_parser):
+	# TODO: Add argements for stream location and trace filters
+	arg_parser.add_argument('-v', '--verbose', action='count', default=0,
+                     		help='Change the verbosity of the logging, each "-v" increases the ammount of logging '
+                           		 'by default this program will log at level INFO ("-vvv"). '
+                        		 '(levels: ERROR, WARN, INFO, DEBUG)')
+
+def set_log_options(parsed_args, root_name):
+    root_log = getLogger(root_name)
+    root_log.setLevel({0: INFO, #default
+                       1: ERROR,
+                       2: WARN,
+                       3: INFO,
+                       4: DEBUG,
+                       5: CALL
+                      }.get(min(5, parsed_args.verbose), 0))
+
+    # Until log stream options are added, we will just use the default stream
+    handler = StreamHandler()
+    handler.setFormatter(Formatter('%(asctime)10s.%(msecs)3d %(levelname)-6s %(name)-24s %(message)s', '%c'))
+    root_log.addHandler(handler)
+
 def log_function(logger, level = CALL):
 	def wrap_func(func):
 		def logged(*args, **kwargs):

File pyshipcommand/server.py

 from ship import ShipMgr
 from djlib.utils import IManager, IGame, IdAllocator, IAllocator
 
-from djlib.logger import getLogger, log_method
+from djlib.logger import getLogger, log_method, add_log_args, set_log_options
 log = getLogger('pyshipcommand.server')
 
 from collections import namedtuple
                       help="Interface to listen for new client connections")
     args.add_argument('-p', '--port', type=int,
                       help="Port to listen for new client connections")
-    args.add_argument('-v', '--verbose', action='count', default=0,
-                      help='Change the verbosity of the logging, each "-v" increases the ammount of logging '
-                           'by default this program will log at level INFO ("-vvv"). '
-                           '(levels: ERROR, WARN, INFO, DEBUG)')
     args.add_argument('-c', '--config', action='append', default=[],
-                      help="The config file used to set up the server")    
+                      help="The config file used to set up the server")
+
+    # Add reusable logging options
+    add_log_args(args)
+
     options = args.parse_args(argv)
 
-    # Logging
-    from djlib import logger
-    root_log = getLogger('pyshipcommand')
-    root_log.setLevel({0: logger.INFO, #default
-                       1: logger.ERROR,
-                       2: logger.WARN,
-                       3: logger.INFO,
-                       4: logger.DEBUG,
-                       5: logger.CALL
-                      }.get(min(5, options.verbose), 0))
-    handler = logger.StreamHandler()
-    handler.setFormatter(logger.Formatter('%(asctime)10s.%(msecs)3d %(levelname)-6s %(name)-24s %(message)s', '%c'))
-    root_log.addHandler(handler)
+    # Apply log options
+    set_log_options(options, 'pyshipcommand')
 
     # Config file
     from ConfigParser import SafeConfigParser as ConfigParser, NoSectionError