Skip to content
Browse files

Reworked logging to avoid clash with 'suds' logs

We now use our own logger 'sms-cli' instead of the root object.
  • Loading branch information...
1 parent 67ae47e commit 0359afb016a3d949a88b2e9011c84718e2c696ae @mludvig committed
Showing with 72 additions and 7 deletions.
  1. +1 −1 Sms/Config.py
  2. +1 −1 Sms/GenericSmsDriver.py
  3. +65 −0 Sms/Logger.py
  4. +1 −1 Sms/Sender.py
  5. +4 −4 sms-cli
View
2 Sms/Config.py
@@ -3,10 +3,10 @@
## License: GPL Version 2
import logging
-from logging import debug, info, warning, error
import re
from Exceptions import *
+from Logger import *
class Config(object):
_instance = None
View
2 Sms/GenericSmsDriver.py
@@ -2,7 +2,7 @@
## http://www.logix.cz/michal/devel/sms-cli
## License: GPL Version 2
-from logging import debug, info
+from Logger import *
from Exceptions import *
class GenericSmsDriver(object):
View
65 Sms/Logger.py
@@ -0,0 +1,65 @@
+## Michal Ludvig <mludvig@logix.net.nz>
+## http://www.logix.cz/michal/devel/sms-cli
+## License: GPL Version 2
+
+import logging
+
+__all__ = []
+
+_logger = logging.getLogger("sms-cli")
+
+def logger_init(level, format = '%(levelname)s: %(message)s'):
+ global _logger
+
+ # create logger
+ _logger = logging.getLogger("sms-cli")
+ _logger.setLevel(level)
+
+ # create console handler and set level
+ ch = logging.StreamHandler()
+ ch.setLevel(logging.DEBUG)
+
+ # create formatter
+ formatter = logging.Formatter(format)
+ ch.setFormatter(formatter)
+
+ # add ch to logger
+ _logger.addHandler(ch)
+
+ # remove parent to avoid double-logging
+ _logger.parent = None
+__all__.append("logger_init")
+
+def logger_set_level(level):
+ _logger.setLevel(level)
+__all__.append("logger_set_level")
+
+def debug(message):
+ _logger.debug(message)
+__all__.append("debug")
+
+def info(message):
+ _logger.info(message)
+__all__.append("info")
+
+def warning(message):
+ _logger.warning(message)
+__all__.append("warning")
+
+def error(message):
+ _logger.error(message)
+__all__.append("error")
+
+def critical(message):
+ _logger.critical(message)
+__all__.append("critical")
+
+if __name__ == "__main__":
+ info("Before loglevel - should not be displayed")
+ logger_init(logging.INFO)
+ info("Default format")
+ logger_init(logging.DEBUG, format='%(levelname)s: %(message)s')
+ debug("Our formar")
+ error("Error, really!")
+
+# vim:et:ts=4:sts=4:ai
View
2 Sms/Sender.py
@@ -2,9 +2,9 @@
## http://www.logix.cz/michal/devel/sms-cli
## License: GPL Version 2
-from logging import debug, error
from Config import Config
from Exceptions import *
+from Logger import *
class SmsSender(object):
def __init__(self, recipients = [], engine_options = {}, **kwargs):
View
8 sms-cli
@@ -12,11 +12,11 @@ import datetime
import logging
from optparse import OptionParser
-from logging import debug, info, warning, error
from Sms.Config import Config
from Sms.Sender import SmsSender
from Sms.Exceptions import *
+from Sms.Logger import *
try:
default_config_file=os.getenv("HOME")+"/.sms-cli.conf"
@@ -40,7 +40,7 @@ optparser.add_option( "--quiet", dest="verbosity", action="store_const", co
## Some mucking with logging levels to enable
## debugging/verbose output for config file parser on request
-logging.basicConfig(level=options.verbosity, format='%(levelname)s: %(message)s')
+logger_init(level = options.verbosity)
## Now finally parse the config file
try:
@@ -59,7 +59,7 @@ except SmsConfigError, e:
## according to configfile and command line parameters
if options.verbosity != default_verbosity:
cfg.verbosity = options.verbosity
-logging.root.setLevel(cfg.verbosity)
+logger_set_level(level = cfg.verbosity)
## Update Config with other parameters
for option in cfg.option_list():
@@ -73,7 +73,7 @@ for option in cfg.option_list():
pass
if not options.message or not options.recipients:
- sys.stderr.write('Message and at least one recipient must be set!\n')
+ error('Message and at least one recipient must be set!')
sys.exit(1)
sms = SmsSender()

0 comments on commit 0359afb

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