Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 71 lines (47 sloc) 3.415 kb
4ee2065 @abedra Initial conversion
abedra authored
1 # Logging
2
3 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:
4
5 :trace, :debug, :info, :warn, :error, :fatal
6
7 Logging occurs with the log macro, or the level-specific convenience macros, which write either directly or via an agent. 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 some ugly and unhelpful text 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.
8
9 ## Usage
10
f5b9255 @abedra Adding a small code example to demonstrate usage
abedra authored
11 The following short example should give you what you need to get started:
12
13 (ns example.core
14 (:use [clojure.tools.logging :only (info error)]))
15
16 (defn divide [one two]
17 (try
18 (info "Calculated" (/ one two))
d670067 @abedra README formatting fix
abedra authored
19 (catch Exception ex
20 (error "There was an error in caculation" ex))))
f5b9255 @abedra Adding a small code example to demonstrate usage
abedra authored
21
4ee2065 @abedra Initial conversion
abedra authored
22 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.
23
24 log4j.rootLogger=WARN, A1
25 log4j.logger.user=DEBUG
26 log4j.appender.A1=org.apache.log4j.ConsoleAppender
27 log4j.appender.A1.layout=org.apache.log4j.PatternLayout
28 log4j.appender.A1.layout.ConversionPattern=%d %-5p %c: %m%n
29
30 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.
31
32 ### "Installation"
33
34 Logging is available in Maven central. Add it to your Maven project's `pom.xml`:
35
36 <dependency>
37 <groupId>org.clojure</groupId>
38 <artifactId>tools.logging</artifactId>
672d927 @abedra updating install instructions
abedra authored
39 <version>0.1.0</version>
4ee2065 @abedra Initial conversion
abedra authored
40 </dependency>
41
42 or your leiningen project.clj:
43
672d927 @abedra updating install instructions
abedra authored
44 [org.clojure/tools.logging "0.1.0"]
4ee2065 @abedra Initial conversion
abedra authored
45
46 Please note the changelog below.
47
48 ### Building Logging
49
50 0. Clone the repo
51 1. Make sure you have maven installed
52 2. Run the maven build; run either:
53 1. `mvn package`: This will produce a logging jar file in the `target`
54 directory, and run all tests with the most recently-released build
55 of Clojure (currently 1.2.0). Or,
56 2. `mvn verify`: This does the same, but also runs the tests with
57 other Clojure "profiles" (currently v1.1.0 and v1.1.0 + clojure-contrib).
58
59 ## Thanks
60
61 * Chris Dean
62 * Phil Hagelberg
63 * Richard Newman
64 * Timothy Pratley
65
66 ## License
67
68 Copyright © 2009 Alex Taggart
69
70 Licensed under the EPL. (See the file epl.html.)
Something went wrong with that request. Please try again.