Kotlin doesn't have the same notion of static fields as used in Java. In Java, the generally accepted way of doing logging is:
public class Foo {
private static final Logger LOG = LoggerFactory.getLogger(Foo.class);
}
Question is what is the idiomatic way of performing logging in Kotlin?
First, you can add extension functions for logger creation.
Then you will be able to create a logger using the following code.
Second, you can define an interface that provides a logger and its mixin implementation.
This interface can be used in the following way.
That's what companion objects are for, in general: replacing static stuff.
As a good example of logging implementation I'd like to mention Anko which uses a special interface
AnkoLogger
which a class that needs logging should implement. Inside the interface there's code that generates a logging tag for the class. Logging is then done via extension functions which can be called inside the interace implementation without prefixes or even logger instance creation.I don't think this is idiomatic, but it seems a good approach as it requires minimum code, just adding the interface to a class declaration, and you get logging with different tags for different classes.
The code below is basically AnkoLogger, simplified and rewritten for Android-agnostic usage.
First, there's an interface which behaves like a marker interface:
It lets its implementation use the extensions functions for
MyLogger
inside their code just calling them onthis
. And it also contains logging tag.Next, there is a general entry point for different logging methods:
It will be called by logging methods. It gets a tag from
MyLogger
implementation, checks logging settings and then calls one of two handlers, the one withThrowable
argument and the one without.Then you can define as many logging methods as you like, in this way:
These are defined once for both logging just a message and logging a
Throwable
as well, this is done with optionalthrowable
parameter.The functions that are passed as
handler
andthrowableHandler
can be different for different logging methods, for example, they can write the log to file or upload it somewhere.isLoggingEnabled
andLoggingLevels
are omitted for brevity, but using them provides even more flexibility.It allows for the following usage:
There is a small drawback: a logger object will be needed for logging in package-level functions:
Anko
You can use
Anko
library to do it. You would have code like below:kotlin-logging
kotlin-logging(Github project - kotlin-logging ) library allows you to write logging code like below:
StaticLog
or you can also use this small written in Kotlin library called
StaticLog
then your code would looks like:The second solution might better if you would like to define an output format for logging method like:
or use filters, for example:
timberkt
If you'd already used Jake Wharton's
Timber
logging library checktimberkt
.Code example:
Check also: Logging in Kotlin & Android: AnkoLogger vs kotlin-logging
Hope it will help
Have a look at the kotlin-logging library.
It allows logging like that:
Or like that:
I also wrote a blog post comparing it to
AnkoLogger
: Logging in Kotlin & Android: AnkoLogger vs kotlin-loggingDisclaimer: I am the maintainer of that library.
Edit: kotlin-logging now has multiplatform support: https://github.com/MicroUtils/kotlin-logging/wiki/Multiplatform-support
Would something like this work for you?