StaticLog
StaticLog is a super lightweight logging library implemented in pure Kotlin (https://kotlinlang.org). It is designed to be used in Kotlin, Java and Android.
It is for formatting standard output comfortably without the need to construct a Logger object. But it's also no problem to create one.
This is an example output in IntelliJ IDEA Yes, the occurrence of the log message is clickable!
Table of Contents
Getting Started
The source/target compatibility is Java 1.6.
This library is uploaded to jCenter and Maven Central.
Gradle / Maven
dependencies {
compile 'io.github.jupf.staticlog:staticlog:2.2.0'
}
<dependency>
<groupId>io.github.jupf.staticlog</groupId>
<artifactId>staticlog</artifactId>
<version>2.1.9</version>
</dependency>
If you dont have the Kotlin runtime already present in your project, use the following dependency.
dependencies {
compile 'io.github.jupf.staticlog:staticlog-java:2.2.0'
}
<dependency>
<groupId>io.github.jupf.staticlog</groupId>
<artifactId>staticlog-java</artifactId>
<version>2.1.9</version>
</dependency>
StaticLog in Kotlin
You can find the example source code here.
Logging in Kotlin
Logging with StaticLog in Kotlin is as easy as it gets:
Log.info("This is an info message")
Log.debug("This is a debug message")
Log.warn("This is a warning message","WithACustomTag")
Log.error("This is an error message with an additional Exception for output", "AndACustomTag", exception )
Log.logLevel = LogLevel.WARN
Log.info("This message will not be shown")
Formatting Output in Kotlin
To define an output format for StaticLog in Kotlin is very easy. It uses a mix from builders and function call syntax.
This defines for example the default format:
Log.newFormat {
line(date("yyyy-MM-dd HH:mm:ss"), space, level, text("/"), tag, space(2), message, space(2), occurrence)
}
You can even define multiple lines and indent them:
Log.newFormat {
line(date("yyyy-MM-dd HH:mm:ss"), space, level, text("/"), tag, space(2), occurrence)
indent {
line(message)
}
}
Tag Filtering in Kotlin
It is possible to filter the output for a specific tag. This is rather easy, just provide the tag:
Log.filterTag = "filterTag"
Log.info("This log will be filtered out", "otherTag")
Log.info("This log has the right tag", "filterTag")
Deleting a tag filter is just as easy:
Log.deleteTagFilter()
Log instances in Kotlin
If you need another log instance you can create one very easy. It can have an own format and log level:
val logger = Log.kotlinInstance()
logger.debug("This message is from an individual logger instance")
The interface of individual log instances is exactly the same as the interface for the static Log class.
FormatBuilders in Kotlin
Here are all possible FormatBuilders:
FormatBuilder | Output |
---|---|
date(Pattern) | Prints the date in the given pattern (The pattern is defined in SimpleDateFormat) |
epoch | Prints the current time in milliseconds |
level | Prints the log level |
tag | Prints the log tag (If none was provided to the logging function, it defaults to the class name the message was logged from.) |
message | Prints the log message |
occurrence | Prints the origin of the logging (In Eclipse and IntelliJ clickable!) |
space | Prints 1 space |
space(X: Integer) | Prints X spaces |
tab | Prints 1 tab |
tab(X: Integer) | Prints X tabs |
text(S: String) | Prints the String S |
Custom Printers in Kotlin
tbd
StaticLog in Java
You can find the example source code here.
Logging in Java
Logging with StaticLog in Kotlin is as easy as it gets:
Log.info("This is an info message");
Log.debug("This is a debug message");
Log.warn("This is a warning message","WithACustomTag");
Log.error("This is an error message with an additional Exception for output", "AndACustomTag", exception );
Log.setLogLevel(LogLevel.WARN);
Log.info("This message will not be shown");
Formatting Output in Java
To define an output format for StaticLog in Java is very easy.
This defines for example the default format:
import static de.jupf.staticlog.Log.FormatOperations.*;
...
LogFormat format = Log.newFormat();
format.line(date("yyyy-MM-dd HH:mm:ss"), space(1), level(), text("/"), tag(), space(2), message(), space(2), occurrence());
You can even define multiple lines and indent them:
LogFormat format = Log.newFormat();
format.line(date("yyyy-MM-dd HH:mm:ss"), space(1), level(), text("/"), tag(), space(2), occurrence());
format.indent(line(message()));
Tag Filtering in Java
It is possible to filter the output for a specific tag. This is rather easy, just provide the tag:
Log.setTagFilter("filterTag");
Log.info("This log will be filtered out", "otherTag");
Log.info("This log has the right tag", "filterTag");
Deleting a tag filter is just as easy:
Log.deleteTagFilter();
Log instances in Java
If you need another log instance you can create one very easy. It can have an own log level and format:
Logger logger = Log.javaInstance();
logger.debug("This message is from an individual logger instance");
The interface of individual log instances is exactly the same as the interface for the static Log class.
FormatBuilders in Java
Here are all possible FormatBuilders:
FormatBuilder | Output |
---|---|
date(Pattern) | Prints the date in the given pattern (The pattern is defined in SimpleDateFormat) |
epoch() | Prints the current time in milliseconds |
level() | Prints the log level |
tag() | Prints the log tag (If none was provided to the logging function, it defaults to the class name the message was logged from.) |
message() | Prints the log message |
occurrence() | Prints the origin of the logging (In Eclipse and IntelliJ clickable!) |
space(X: Integer) | Prints X spaces |
tab(X: Integer) | Prints X tabs |
text(S: String) | Prints the String S |
Custom Printers in Java
tbd
StaticLog in Android
StaticLog automatically detects Android VMs and switches its output to the Android logger.
The default format for Android is defined like this:
format.line(message(), space(2), occurrence());
The tag is forwarded to the Android logger. If none is provided, it defaults to the class name the log was printed from.
For further questions look at StaticLog in Java