API for logging - clojure-contrib next (in development)

by Alex Taggart, with contributions and suggestions by Chris Dean, Phil Hagelberg, Richard Newman, and Timothy Pratley

Usage:
(ns your-namespace
  (:require clojure.contrib.logging))

Overview

Logging macros which delegate to a specific logging implementation. At
runtime a specific implementation is selected from, in order, Apache
commons-logging, slf4j, log4j, and finally java.util.logging.

Logging levels are specified by clojure keywords corresponding to the
values used in log4j and commons-logging:
  :trace, :debug, :info, :warn, :error, :fatal

Logging occurs with the log macro, or the level-specific convenience macros,
which write either directly or via an agent.  See log* for more details
regarding direct vs agent logging.

The log macros will not evaluate their 'message' unless the specific logging
level is in effect. Alternately, you can use the spy macro when you have code
that needs to be evaluated, and also want to output the code and its result to
the log.

Unless otherwise specified, the current namespace (as identified by *ns*) will
be used as the log-ns (similar to how the java class name is usually used).
Note: your log configuration should display the name that was passed to the
logging implementation, and not perform stack-inspection, otherwise you'll see
something like "fn__72$impl_write_BANG__39__auto____81" in your logs.

Use the enabled? macro to write conditional code against the logging level
(beyond simply whether or not to call log, which is handled automatically).

You can redirect all java writes of System.out and System.err to the log
system by calling log-capture!.  To bind *out* and *err* to the log system
invoke with-logs.  In both cases a log-ns (e.g., "com.example.captured")
must be specified in order to namespace the output.

For those new to using a java logging library, the following is a very basic
configuration for log4j. Place it in a file called "log4j.properties"
and place that file (and the log4j JAR) on the classpath.
  log4j.rootLogger=WARN, A1
  log4j.logger.user=DEBUG
  log4j.appender.A1=org.apache.log4j.ConsoleAppender
  log4j.appender.A1.layout=org.apache.log4j.PatternLayout
  log4j.appender.A1.layout.ConversionPattern=%d %-5p %c: %m%n
The above will print messages to the console for :debug or higher if one is
in the user namespace, and :warn or higher in all other namespaces.

Public Variables and Functions



*allow-direct-logging*

var

  
A boolean indicating whether direct logging (as opposed to via an agent) is
allowed when not operating from within a transaction. Defaults to true.


*force*

var

  
Overrides the default rules for choosing between logging directly or via an
agent. Defaults to nil. See log* for details.
Source


*impl-name*

var

  
The name of the logging implementation used.


*log-factory*

var

  
An instance satisfying the LogFactory protocol. Used internally when needing
to obtain an instance satisfying the Log protocol. Defaults to the value
returned from find-factory. Can be rebound to provide alternate logging
implementations
Source


*logging-agent*

var

  
The default agent used for performing logging durng a transaction or when
direct logging is disabled.


*tx-agent-levels*

var

  
The set of levels that will require using an agent when logging from within a
running transaction. Defaults to #{:info :warn}. See log* for details.
Source


Log

var

  
The protocol through which macros will interact with an underlying logging
implementation.  Implementations should at least support the six specified
logging levels if they wish to benefit from the level-specific macros.
Source


LogFactory

var

  
The protocol through which macros will obtain an instance satisfying Log as
well as providing information about the particular implementation being used.
Implementations should be bound to *log-factory* in order to be picked up by
this library.
Source


debug

macro
Usage: (debug message)
       (debug message throwable)
Logs a message at the debug level.


debugf

macro
Usage: (debugf fmt & fmt-args)
       (debugf throwable fmt & fmt-args)
Debug level logging using format.
Source


enabled?

macro
Usage: (enabled? level)
       (enabled? level log-ns)
Returns true if the specific logging level is enabled.  Use of this function
should only be necessary if one needs to execute alternate code paths beyond
whether the log should be written to.


error

macro
Usage: (error message)
       (error message throwable)
Logs a message at the error level.


errorf

macro
Usage: (errorf fmt & fmt-args)
       (errorf throwable fmt & fmt-args)
Error level logging using format.
Source


fatal

macro
Usage: (fatal message)
       (fatal message throwable)
Logs a message at the fatal level.


fatalf

macro
Usage: (fatalf fmt & fmt-args)
       (fatalf throwable fmt & fmt-args)
Fatal level logging using format.
Source


find-factory

function
Usage: (find-factory)
Returns the first LogFactory found that is available from commons-logging,
slf4j-logging, log4j-logging, or java-util-logging. End-users should not need
to call this.
Source


impl-enabled?

var

  
Implementation-specific check if a particular level is enabled. End-users
should not need to call this.


impl-get-log

var

  
Returns an implementation-specific log by string namespace. End-users should
not need to call this.


impl-name

function
Usage: (impl-name factory)
Returns some text identifying the underlying implementation.


impl-write!

var

  
Implementation-specific write of a log message. End-users should not need to
call this.


info

macro
Usage: (info message)
       (info message throwable)
Logs a message at the info level.


infof

macro
Usage: (infof fmt & fmt-args)
       (infof throwable fmt & fmt-args)
Info level logging using format.
Source


java-util-logging

function
Usage: (java-util-logging)
Returns a java.util.logging-based implementation of the LogFactory protocol,
or nil if not available. End-users should not need to call this.
Source


log

macro
Usage: (log level message)
       (log level message throwable)
       (log level message throwable log-ns)
Logs a message, either directly or via an agent. Also see the level-specific
convenience macros.


log*

function
Usage: (log* log level throwable message)
Attempts to log a message, either directly or via an agent; does not check if
the level is enabled.

For performance reasons, an agent will only be used when invoked within a
running transaction, and only for logging levels specified by
*tx-agent-levels*. This allows those entries to only be written once the
transaction commits, and are discarded if it is retried or aborted.  As
corollary, other levels (e.g., :debug, :error) will be written even from
failed transactions though at the cost of repeat messages during retries.

One can override the above by setting *force* to :direct or :agent; all
subsequent writes will be direct or via an agent, respectively.
Source


log-capture!

function
Usage: (log-capture! log-ns)
Captures System.out and System.err, redirecting all writes of those streams
to :info and :error logging, respectively. The specified log-ns value will
be used to namespace all redirected logging. NOTE: this will not redirect
output of *out* or *err*; for that, use with-logs.


log-stream

function
Usage: (log-stream level log-ns)
Creates a PrintStream that will output to the log. End-users should not need
to invoke this.


log-uncapture!

function
Usage: (log-uncapture!)
Restores System.out and System.err to their original values.


logf

macro
Usage: (logf level fmt & fmt-args)
       (logf level throwable fmt & fmt-args)
Logs a message using a format string and args. Can optionally take a
throwable as its second arg. See level-specific macros, e.g., debugf.
Source


logp

macro
Usage: (logp level message & more)
       (logp level throwable message & more)
Logs a message using print style args. Can optionally take a throwable as its
second arg. See level-specific macros, e.g., debug.
Source


slf4j-logging

function
Usage: (slf4j-logging)
Returns a SLF4J-based implementation of the LogFactory protocol, or nil if
not available. End-users should not need to call this.
Source


spy

macro
Usage: (spy expr)
Evaluates expr and outputs the form and its result to the debug log; returns 
the result of expr.


trace

macro
Usage: (trace message)
       (trace message throwable)
Logs a message at the trace level.


tracef

macro
Usage: (tracef fmt & fmt-args)
       (tracef throwable fmt & fmt-args)
Trace level logging using format.
Source


warn

macro
Usage: (warn message)
       (warn message throwable)
Logs a message at the warn level.


warnf

macro
Usage: (warnf fmt & fmt-args)
       (warnf throwable fmt & fmt-args)
Warn level logging using format.
Source


with-logs

macro
Usage: (with-logs log-ns & body)
Evaluates exprs in a context in which *out* and *err* are bound to :info and
:error logging, respectively. The specified log-ns value will be used to
namespace all redirected logging.
Logo & site design by Tom Hickey.
Clojure auto-documentation system by Tom Faulhaber.