clojure libs and logging

51 views
Skip to first unread message

Keith Irwin

unread,
Nov 22, 2011, 12:10:31 AM11/22/11
to clo...@googlegroups.com
Folks--

I'm working on an experimental library which interfaces with external resources (i.e., not purely functional) and I'd like the library to log things when stuff goes wrong (say, with network connections). I don't want to throw exceptions and let clients handle it because I want to build in some automated reconnect stuff.

Normally, I use clojure.tools.logging and add dependencies for log back. Works great.

What's the standard way for including logging in a library? I've included clojure.tools.logging as a dependency, but the resulting jar (lein install), contains clojure.tools.logging classes. Is that okay, given any project its likely to be included in will also depend on clojure.tools.logging?

Is there some way to do this? Is embedded logging (like Jetty has, for instance), just Not Done in the clojure library world?

Keith

Luc Prefontaine

unread,
Nov 22, 2011, 10:16:37 AM11/22/11
to clo...@googlegroups.com
Hi,

Do you have the :disable-implicit-clean option set to false in your project.clj file ?
If not, add it to remove the classes from dependencies that otherwise may pollute your target.

As far as "standard" logging in a lib, it might be time to rely on clojure.tools.logging
systematically.

We do not use it here yet but at some point it will become unavoidable.

Luc P.

--
Luc P.

================
The rabid Muppet

Keith Irwin

unread,
Nov 22, 2011, 12:22:08 PM11/22/11
to clo...@googlegroups.com
I'm guessing that removing the non-project classes and then just declaring a dependency should work well enough.

Couldn't find :disable-implicit-clean in the sample project file technomancy maintains on his github repo, but did use :clean-non-project-classes, which removed the compiled logging classes from the jar file but kept my own.

However, I inspected a few of the jars in my ~/.m2 directory for other projects and realized people ship libraries as pure clj source files. Neat!

The only way I could get this to work was to remove the :main setting from project.clj. After doing that, "lein install" and "lein jar" just include the source, which is good enough for me (and results in a much smaller jar file).

To run my small long/running "main" test/dev code, I can use :run-alias.

Then,

   lein trampoline run :main

does the trick. (I use trampoline so that shutdown hooks get properly invoked).

Thanks!

K

Phil Hagelberg

unread,
Nov 22, 2011, 1:57:47 PM11/22/11
to clo...@googlegroups.com
On Mon, Nov 21, 2011 at 9:10 PM, Keith Irwin <keith...@gmail.com> wrote:
> What's the standard way for including logging in a library? I've included
> clojure.tools.logging as a dependency, but the resulting jar (lein install),
> contains clojure.tools.logging classes. Is that okay, given any project its
> likely to be included in will also depend on clojure.tools.logging?

This is just a quirk of the Clojure compiler; there's currently no way
to compile a namespace without compiling all its dependencies:
http://dev.clojure.org/jira/browse/CLJ-322

Leiningen includes a workaround for this; you can set
:clean-non-project-classes in project.clj, but it's not on by default
since there are edge cases with protocols where it breaks. The option
Luc mentioned is from an older version where it was turned on by
default.

It's best to avoid AOT compilation unless you need it. You can use the
-m option to lein run to avoid having to declare your -main namespaces
in project.clj:

$ lein run -m clojure.main

-Phil

Luc Prefontaine

unread,
Nov 22, 2011, 2:38:37 PM11/22/11
to clo...@googlegroups.com
Oups, I'll look at it... we may have well upgraded Lein without reviewing project.clj options.
We're about to deliver another version of our software so it's just about time to do that.

--

Reply all
Reply to author
Forward
0 new messages