Skip to content

Commit

Permalink
Merge pull request #2 from GoodforGod/dev
Browse files Browse the repository at this point in the history
[0.11.0]
  • Loading branch information
GoodforGod authored Mar 11, 2022
2 parents 0c1c557 + 1afdce4 commit aa0afb7
Show file tree
Hide file tree
Showing 38 changed files with 2,730 additions and 1,062 deletions.
18 changes: 17 additions & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,30 @@ root = true
end_of_line = lf
charset = utf-8

# Json
[*.json]
indent_size = 2
indent_style = space
insert_final_newline = false
trim_trailing_whitespace = true

# Yaml
[{*.yml, *.yaml}]
indent_size = 2
indent_style = space
insert_final_newline = true
trim_trailing_whitespace = true

# Property files
[*.properties]
indent_size = 2
indent_style = space
insert_final_newline = true
trim_trailing_whitespace = true


# XML files
[*.xml]
indent_size = 4
indent_style = space
insert_final_newline = true
trim_trailing_whitespace = true
35 changes: 27 additions & 8 deletions .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@
# and leave all files detected as binary untouched.
* text=auto

#

# The above will handle all files NOT found below
#
# These files are text and should be normalized (Convert crlf => lf)
*.bash text eol=lf
*.css text diff=css
Expand All @@ -26,16 +25,36 @@
*.xml text
*.yml text eol=lf


# These files are binary and should be left untouched
# (binary is a macro for -text -diff)
*.class binary
# Archives
*.7z binary
*.br binary
*.gz binary
*.tar binary
*.zip binary
*.jar binary
*.so binary
*.war binary
*.dll binary
*.ear binary
*.gif binary

# Documents
*.pdf binary

# Images
*.ico binary
*.jar binary
*.gif binary
*.jpg binary
*.jpeg binary
*.png binary
*.so binary
*.war binary
*.psd binary
*.webp binary

# Fonts
*.woff2 binary

# Other
*.exe binary
*.class binary
*.ear binary
9 changes: 5 additions & 4 deletions .github/workflows/gradle.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
java: [ '11' ]
java: [ '11', '17' ]
name: Java ${{ matrix.java }} setup

steps:
Expand All @@ -25,16 +25,17 @@ jobs:
with:
java-version: ${{ matrix.java }}

- name: Code Style
run: ./gradlew spotlessCheck

- name: Build
run: ./gradlew classes

- name: Codestyle
run: ./gradlew spotlessCheck

- name: Test
run: ./gradlew test jacocoTestReport

- name: SonarQube
if: matrix.java == '17'
run: ./gradlew sonarqube
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
Expand Down
9 changes: 5 additions & 4 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Package Files #
### Package Files
*.war
*.nar
*.ear
Expand All @@ -9,9 +9,10 @@
### Gradle template
.gradle
build/
target/

# Idea generatted files
### Idea generated files
.idea
/out/
*.iml
.settings/
*.iml
out/
196 changes: 173 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,59 +6,209 @@
[![Maintainability Rating](https://sonarcloud.io/api/project_badges/measure?project=GoodforGod_slf4j-simple-logger&metric=sqale_rating)](https://sonarcloud.io/dashboard?id=GoodforGod_slf4j-simple-logger)
[![Lines of Code](https://sonarcloud.io/api/project_badges/measure?project=GoodforGod_slf4j-simple-logger&metric=ncloc)](https://sonarcloud.io/dashboard?id=GoodforGod_slf4j-simple-logger)

SLF4J library simple logger implementation.
[SLF4J](https://github.com/qos-ch/slf4j) based, simple, efficient logger.

Features:
- Performance optimizations.
- Logger name abbreviation (logback analog).
- Environment variables logging.
- Global logger level change.
- Split output for errors and other.
- GraalVM friendly.
- Simple!

And more.

## Dependency :rocket:

Java 11+ compatible.

[**Gradle**](https://mvnrepository.com/artifact/io.goodforgod/slf4j-simple-logger)
```groovy
dependencies {
implementation "io.goodforgod:slf4j-simple-logger:0.10.0"
}
implementation "io.goodforgod:slf4j-simple-logger:0.11.0"
```

[**Maven**](https://mvnrepository.com/artifact/io.goodforgod/slf4j-simple-logger)
```xml
<dependency>
<groupId>io.goodforgod</groupId>
<artifactId>slf4j-simple-logger</artifactId>
<version>0.10.0</version>
<version>0.11.0</version>
</dependency>
```

Based on SLF4J 1.7.36

## Log Example

Below is example of logged message:
```java
logger.debug("Message is printed for this logger");
```

And the detailed result:
```text
Date Time Implementation Log Level Environment variables Thread Logger Name Log Message
| | | | | | |
___________|__________ ______|_______ __|__ ___________________|______________ __|__ ___________|___________ ________________|________________
| | | | | | | | | | | | | |
| | | | | | | | | | | | | |
2022-02-23T15:43:40.331 [0.9.0-SNAPSHOT] [DEBUG] [SESSION=Console, PROCESSOR_LEVEL=6] [main] io.goodforgod.Application - Message is printed for this logger
```

## Features

Library have same features as default SLF4J simple logger implementation except:
- Library adds set default log level capability for *SimpleLoggerFactory*
### Performance optimizations

## Compatability
This implementation is based on default *slf4j-simple-logger*, but there are plenty of performance and feature improvements.

Library is compatible with SLF4J version *1.7.32*.
Some cases are 100% faster others are even 800% faster, you can read more about here in my [JVM benchmark](https://github.com/GoodforGod/java-logger-benchmark).

Java 11+ compatible.
### DateTime output

## Comfiguration
There are three options to output date & time:
1) DATE_TIME - in format *uuuu-MM-dd'T'HH:mm:ss.SSS*, example is - *2022-02-23T15:43:40.331* (read more about Java [Date & Time formats here](https://goodforgod.dev/posts/2/))
2) TIME - in format *HH:mm:ss.SSS*, example is - *15:43:40.331* (read more about Java [Date & Time formats here](https://goodforgod.dev/posts/2/))
3) UNIX_TIME - time [since epoch](https://en.wikipedia.org/wiki/Unix_time).
4) MILLIS_FROM_START - Millis from SimpleLoggerConfiguration initialization (may not properly work in GraalVM setups)

Example of *simplelogger.properties* file for boostrap your project.
You can also change formatter for DATE_TIME and TIME via configuration:
```properties
org.slf4j.simpleLogger.dateTimeFormat=uuuu-MM-dd'T'HH:mm:ss.SSS
```

### Logger name abbreviation

There is configuration to abbreviate logger name, like in logback.

If configuration is:
```properties
# Default logging detail level for all instances of SimpleLogger. Must be one of ("trace", "debug", "info", "warn", or "error").
org.slf4j.simpleLogger.defaultLogLevel=info
org.slf4j.simpleLogger.showShortLogName=false
org.slf4j.simpleLogger.showLogName=true
org.slf4j.simpleLogger.logNameLength=36
```

# Set to true if you want the current date and time to be included in output messages.
org.slf4j.simpleLogger.showDateTime=true
And logger is:
```java
package io.goodforgod.internal.logger.example;

import org.slf4j.LoggerFactory;
import org.slf4j.Logger;

public class Application {

public static void main(String[] args) {
Logger logger = LoggerFactory.getLogger(Application.class);
logger.info("Message is printed for this logger");
}
}
```

Then logger name will be outputted like:
```text
2022-02-23T15:43:40.331 [DEBUG] i.g.i.logger.example.Application - Message is printed for this logger
```

Abbreviation happened to full logger name:
*io.goodforgod.internal.logger.example.Application* -> *i.g.i.logger.example.Application*

# The date and time format to be used in the output messages. The pattern describing the date and time format is the same that is used in java.text.SimpleDateFormat. If the format is not specified or is invalid, will output the number of milliseconds elapsed since startup.
org.slf4j.simpleLogger.dateTimeFormat=yyyy-MM-dd'T'HH:mm:ss.SSS
### Environment logging

# Set to true if you want to output the current thread name.
It is possible to log environment, format how it will be outputted can be seen [here](#log-example).

If the configuration is:
```properties
# Set environment names to show in output. Envs will be printed out in order they preserve in configuration.
org.slf4j.simpleLogger.environments=SESSION,PROCESSOR_LEVEL
# Set to true to show environment with nullable values.
org.slf4j.simpleLogger.environmentShowNullable=false
# Set to true to show environment names.
org.slf4j.simpleLogger.environmentShowName=true
# Set to true to caches environment values on configuration initialization and then always uses them when logging.
org.slf4j.simpleLogger.environmentRememberOnStart=false
```

Then the output will be:
```text
2022-02-23T15:43:40.331 [DEBUG] [SESSION=Console, PROCESSOR_LEVEL=6] [main] io.goodforgod.Application - Message is printed for this logger
```

Environment variables are printed in order they preserve in configuration (in example above first SESSION, then PROCESSOR_LEVEL).

### Output split

There is possibility to split *WARN* and *ERROR* logs to different output.

If configuration is:
```properties
# Set default logger output file or System.out or System.error (default System.out)
org.slf4j.simpleLogger.logFile=System.out
# Set logger WARN logs output file or System.out or System.error (default System.out)
org.slf4j.simpleLogger.logFileWarn=System.error
# Set logger ERROR logs output file or System.out or System.error (default System.out)
org.slf4j.simpleLogger.logFileError=System.error
```

Then all logs of TRACE, DEBUG, INFO will be forwarded to *System.out* and all WARN & ERROR logs will be forwarded to *System.error*.

### Logger level change

You can change loggers level using *io.goodforgod.slf4j.simplelogger.SimpleLoggerFactory*:
```java
SimpleLoggerFactory factory = (SimpleLoggerFactory) LoggerFactory.getILoggerFactory();
factory.setLogLevel(Level.DEBUG);
```

Or you can use predicate to filter out loggers:
```java
SimpleLoggerFactory factory = (SimpleLoggerFactory) LoggerFactory.getILoggerFactory();
factory.setLogLevel(Level.DEBUG, logger -> logger.getName().startsWith("io.goodforgod.internal.logger.example"));
```

## Configuration

Library support all options of *slf4j-simple-logger*, you can check them [here](https://www.slf4j.org/api/org/slf4j/impl/SimpleLogger.html).

Example of full *simplelogger.properties* file:
```properties
# Default logging level for all loggers. Must be one of ("TRACE", "DEBUG", "INFO", "WARN", or "ERROR"). (default INFO)
org.slf4j.simpleLogger.defaultLogLevel=INFO
# Set to true to show current datetime in output. (default true)
org.slf4j.simpleLogger.showDateTime=true
# Set datetime output type. Must be one of ("TIME", "DATE_TIME", "UNIX_TIME", "MILLIS_FROM_START"). (default DATE_TIME)
org.slf4j.simpleLogger.dateTimeOutputType=DATE_TIME
# The date and time formatter pattern to be used in the output. (default uuuu-MM-dd'T'HH:mm:ss.SSS)
org.slf4j.simpleLogger.dateTimeFormat=uuuu-MM-dd'T'HH:mm:ss.SSS
# Set to true if to show application implementation version from MANIFEST.MF (default false)
org.slf4j.simpleLogger.showImplementationVersion=false
# Set to true to show logging level in brackets like: [INFO] (default true)
org.slf4j.simpleLogger.levelInBrackets=true
# Set to true if to show current thread in output. (default false)
org.slf4j.simpleLogger.showThreadName=false
# Set to true if you want the Logger instance name to be included in output messages.
# Set to true to show only class name in output. (default false)
org.slf4j.simpleLogger.showShortLogName=false
# Set to true if to show full class name in output (package + class name). (default true)
org.slf4j.simpleLogger.showLogName=true
# Set to true if you want the last component of the name to be included in output messages.
org.slf4j.simpleLogger.showShortLogName=true
org.slf4j.simpleLogger.levelInBrackets=true
# Set maximum logger name to output and abbreviate if it exceeds length. (default null)
org.slf4j.simpleLogger.logNameLength=36
# Set environment names to show in output. Envs will be printed out in order they preserve in configuration. (default null)
org.slf4j.simpleLogger.environments=SESSION_ID,ORIGIN,HOST
# Set to true to show environment with nullable values. (default false)
org.slf4j.simpleLogger.environmentShowNullable=false
# Set to true to show environment names. (default false)
org.slf4j.simpleLogger.environmentShowName=false
# Set to true to caches environment values on configuration initialization and then always uses them when logging. (default false)
org.slf4j.simpleLogger.environmentRememberOnStart=false
# Set default logger output file or System.out or System.error (default System.out)
org.slf4j.simpleLogger.logFile=System.out
# Set logger WARN logs output file or System.out or System.error (default System.out)
org.slf4j.simpleLogger.logFileWarn=System.out
# Set logger ERROR logs output file or System.out or System.error (default System.out)
org.slf4j.simpleLogger.logFileError=System.out


# Set log level for custom loggers
org.slf4j.simpleLogger.log.path.to.class=WARN
```

## License
Expand Down
Loading

0 comments on commit aa0afb7

Please sign in to comment.