LittleLogger - A very simple Java/Scala logging utility
A lot of times when I’m writing small test applications I want/need a logger, but I don’t want to deal with the complexities of traditional Java/Scala logging utilities. This morning I finally took a little time to write a very simple logger that I can use in these small applications and test projects. I call it LittleLogger, and I’ll share the Scala source code here today.
LittleLogger.scala
There is only one class in my logger, and it’s named LittleLogger.scala. Without the comments, the source code looks like this:
package com.valleyprogramming.littlelogger import java.io._ import java.util.Calendar import java.text.SimpleDateFormat class LittleLogger(identifier: String) { val timeFormatter = new SimpleDateFormat("hh:mm:ss:SSS") def init(filename: String) { LittleLogger.filename = filename val file = new File(filename) file.createNewFile } def log(msg: String) { if (LittleLogger.filename == null) { System.err.println("LittleLogger: `filename` was null, not going to write anything") return } val bw = new BufferedWriter(new FileWriter(new File(LittleLogger.filename), true)) bw.write(s"$identifier | $getTime | $msg\n") bw.close } private def getTime = timeFormatter.format(Calendar.getInstance.getTime) } object LittleLogger { private var filename: String = _ def apply(identifier: String): LittleLogger = new LittleLogger(identifier) }
As you can see, I intentionally do some things wrong here, primarily the use of null
values. That’s a big no-no in production Scala applications, but because I want to enforce the notion that this logger shouldn’t be used in production applications I’m leaving those null
values in there.
Usage
LittleLogger is easy to use. The following “driver” class shows how to initialize and use it in multiple Scala classes:
object MainDriver extends App { val logger = LittleLogger("MainDriver") logger.init("/Users/Al/Projects/Scala/LittleLogger/log.out") new Foo new Bar } class Foo { val logger = LittleLogger(this.getClass.getName) logger.log("made it to A") logger.log("made it to B") } class Bar { val logger = LittleLogger("Bar") logger.log("made it to A") logger.log("made it to B") }
The output from this driver/test application looks like this:
com.valleyprogramming.littlelogger.Foo | 10:27:47:032 | made it to A com.valleyprogramming.littlelogger.Foo | 10:27:47:033 | made it to B Bar | 10:27:47:060 | made it to A Bar | 10:27:47:061 | made it to B
You’ll want a lot more functionality than this for production projects, but for little one-off projects, this is all I need.
More information and project source code
That’s really all there is to the LittleLogger. I was driven to create this little project by two feelings:
- I got tired of trying to find Java/Scala
System.out.println
output on Mac OS X systems. - I don’t like the complexity of traditional logging utilities for simple/little projects.
You can find more information and the complete project source code on Github at this URL: github.com/alvinj/LittleLogger
One final note: Please don’t use this code in production applications, it’s really not intended for that sort of thing.
Reporting live from Boulder, Colorado with more Scala source code, this is Alvin Alexander.
Recent blog posts
- Free Scala and functional programming video training courses
- Free: Introduction To Functional Programming video training course
- The #1 functional programming (and computer programming) book
- The User Story Mapping Workshop process
- Alvin Alexander, Certified Scrum Product Owner (CSPO)
- Alvin Alexander is now a Certified ScrumMaster (CSM)
- Our “Back To Then” app (for iOS and Android)
- A Docker cheat sheet
- Pushing a Scala 3 JAR/Docker file to Google Cloud Run
- Reading a CSV File Into a Spark RDD (Scala Cookbook recipe)