Created
May 2, 2012 22:59
-
-
Save sptramer/2581199 to your computer and use it in GitHub Desktop.
CLI logger support
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* NOTE: No packages for log, logger, log4j, etc. listed in NPM. | |
* Assuming no log4j-style support for node exists. | |
* | |
* NOTE: Not quite true. node-log4j is a project (1m old, "compatible with node 0.4") | |
* https://github.com/nomiddlename/log4js-node | |
* | |
* This might be suitable but because of the lack of a package or regular maintenance | |
* we may have difficulty maintaining it. | |
* | |
* ---- | |
* | |
* Due to the second requirement, we cannot use console() (requires | |
* "multiple loggers") and would have to instead homebrew a Logger class | |
* which would use multiple node APIs (probably all stable: 3) and probably | |
* distribute it as an npm package. | |
* | |
* This is a time-intensive task and falls outside of the scope of | |
* a side-by-side. | |
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import logging | |
import sys | |
# Changing the StreamHandler() to a FileHandler() lets us log to a file, | |
# there are also RotatingFileHandler() and TimedRotatingFileHandler() | |
# | |
# See also the logging cookbook for things like multiple handlers: | |
# http://docs.python.org/howto/logging-cookbook.html#logging-cookbook | |
log = logging.getLogger('stdout') | |
log.setLevel(logging.WARN) | |
handler = logging.StreamHandler(sys.stdout) | |
handler.setFormatter(logging.Formatter("[%(levelname)s](%(processName)s --- %(asctime)s) %(message)s")) | |
log.addHandler(handler) | |
log.warn('oops!') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require 'logger' | |
# The second test for ruby is not required; Logger.new() takes | |
# a filename or IO object as part of its constructor, as well | |
# as log refresh/rotation options. | |
logger = Logger.new(STDOUT) | |
logger.level = Logger::WARN #warn to stdout | |
logger.progname = $0 | |
logger.formatter = proc do |severity, datetime, progname, msg| | |
"[%s](%s --- %s) %s\n" % [severity, progname, datetime, msg] | |
end | |
logger.warn("oops!") | |
logger.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment