Python: logging module - globally?

Use logging. GetLogger(name) to create a named global logger. ( docs.python.org/py3k/library/logging.htm... ) main.Py import log logger = log.

Setup_custom_logger('root') logger. Debug('main message') import submodule log.Py import logging def setup_custom_logger(name): formatter = logging. Formatter(fmt='%(asctime)s - %(levelname)s - %(module)s - %(message)s') handler = logging.StreamHandler() handler.

SetFormatter(formatter) logger = logging. GetLogger(name) logger. SetLevel(logging.

DEBUG) logger. AddHandler(handler) return logger submodule. Py import logging logger = logging.

GetLogger('root') logger. Debug('submodule message') Output 2011-10-01 20:08:40,049 - DEBUG - main - main message 2011-10-01 20:08:40,050 - DEBUG - submodule - submodule message.

Create an instance of customLogger in your log module and use it as a singleton - just use the imported instance, rather than the class.

You can just pass it a string with a common sub-string before the first period. The parts of the string separated by the period (". ") can be used for different classes / modules / files / etc.Like so (specifically the logger = logging.

GetLogger(loggerName) part): def getLogger(name, logdir=LOGDIR_DEFAULT, level=logging. DEBUG, logformat=FORMAT): base = os.path. Basename(__file__) loggerName = "%s.

%s" % (base, name) logFileName = os.path. Join(logdir, "%s. Log" % loggerName) logger = logging.

GetLogger(loggerName) logger. SetLevel(level) I = 0 while os.path. Exists(logFileName) and not os.

Access(logFileName, os. R_OK | os. W_OK): I += 1 logFileName = "%s.

%s. Log" % (logFileName. Replace(".

Log", ""), str(i). Zfill((len(str(i)) + 1))) try: #fh = logging. FileHandler(logFileName) fh = RotatingFileHandler(filename=logFileName, mode="a", maxBytes=1310720, backupCount=50) except IOError, exc: errOut = "Unable to create/open log file \"%s\".

" % logFileName if exc. Errno is 13: # Permission denied exception errOut = "ERROR ** Permission Denied ** - %s" % errOut elif exc. Errno is 2: # No such directory errOut = "ERROR ** No such directory \"%s\"** - %s" % (os.path.

Split(logFileName)0, errOut) elif exc. Errno is 24: # Too many open files errOut = "ERROR ** Too many open files ** - Check open file descriptors in /proc//fd/ (PID: %s)" % os.getpid() else: errOut = "Unhandled Exception ** %s ** - %s" % (str(exc), errOut) raise LogException(errOut) else: formatter = logging. Formatter(logformat) fh.

SetLevel(level) fh. SetFormatter(formatter) logger. AddHandler(fh) return logger class MainThread: def __init__(self, cfgdefaults, configdir, pidfile, logdir, test=False): self.

Logdir = logdir logLevel = logging. DEBUG logPrefix = "MainThread_TEST" if self. Test else "MainThread" try: self.

Logger = getLogger(logPrefix, self. Logdir, logLevel, FORMAT) except LogException, exc: sys.stderr. Write("%s\n" % exc) sys.stderr.flush() os.

_exit(0) else: self.logger. Debug("-------------------- MainThread created. Starting __init__() --------------------") def run(self): self.logger.

Debug("Initializing ReportThreads..") for (group, cfg) in self.config.items(): self.logger. Debug(" ------------------------------ GROUP '%s' CONFIG ------------------------------ " % group) for k2, v2 in cfg.items(): self.logger. Debug("%s %s: %s" % (group, k2, v2)) try: rt = ReportThread(self, group, cfg, self.

Logdir, self. Test) except LogException, exc: sys.stderr. Write("%s\n" % exc) sys.stderr.flush() self.logger.

Exception("Exception when creating ReportThread (%s)" % group) logging.shutdown() os. _exit(1) else: self.threads. Append(rt) self.logger.

Debug("Threads initialized.. \"%s\"" % ", ". Join(t.Name for t in self. Threads)) for t in self.

Threads: t.Start() if not self. Test: self.loop() class ReportThread: def __init__(self, mainThread, name, config, logdir, test): self. MainThread = mainThread self.

Name = name logLevel = logging. DEBUG self. Logger = getLogger("MainThread%s.

ReportThread_%s" % ("_TEST" if self. Test else "", self. Name), logdir, logLevel, FORMAT) self.logger.

Info("init database...") self.initDB() # etc.... if __name__ == "__main__": # ..... MainThread(cfgdefaults=options. Cfgdefaults, configdir=options. Configdir, pidfile=options.

Pidfile, logdir=options. Logdir, test=options. Test).

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions


Thank You!
send