Skip to content

Instantly share code, notes, and snippets.

@sptramer
Created May 2, 2012 22:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sptramer/2581199 to your computer and use it in GitHub Desktop.
Save sptramer/2581199 to your computer and use it in GitHub Desktop.
CLI logger support
/*
* 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.
*/
#!/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!')
#!/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