All Projects → ffissore → slf4j-fluent

ffissore / slf4j-fluent

Licence: MIT license
A fluent api for SLF4J

Programming Languages

java
68154 projects - #9 most used programming language

Projects that are alternatives of or similar to slf4j-fluent

Skill6 Website Backend
java进阶开发,高级版web项目。基于dubbo实现分布式微服务架构,基于spring boot、spring mvc、mybatis、docker、ELK、redis、vue.js、node.js等技术实现的技术分享交流网站。网站名称:技术流,英文名:skill6。主要功能有:登录注册、单点登录、restful设计、文章及评论、代码及资源下载、主题讨论等。持续更新中...
Stars: ✭ 140 (+636.84%)
Mutual labels:  slf4j
BOBBIN
Revolutionary high-performance Groovy/Java Slf4j logger
Stars: ✭ 17 (-10.53%)
Mutual labels:  slf4j
website
Fully responsive website built with NextJS, React and Fluent UI, with the aim of providing services and access to all groups of didactic courses and general purposes to students of the University of Milan.
Stars: ✭ 29 (+52.63%)
Mutual labels:  fluent
Terse Logback
Structured Logging, Tracing, and Observability with Logback
Stars: ✭ 146 (+668.42%)
Mutual labels:  slf4j
slf4j-timber
SLF4J binding for Timber - a logger with a small, extensible API which provides utility on top of Android's normal Log class.
Stars: ✭ 20 (+5.26%)
Mutual labels:  slf4j
angular-odata-es5
OData Service for Angular.io (es5 version)
Stars: ✭ 45 (+136.84%)
Mutual labels:  fluent
Zio Logging
Simple logging for ZIO apps, with correlation, context & pluggable backends out of the box.
Stars: ✭ 85 (+347.37%)
Mutual labels:  slf4j
FluentExcel
Use Fluent API to configure POCO excel behaviors, and then provides IEnumerable<T> has save to and load from excel functionalities.
Stars: ✭ 73 (+284.21%)
Mutual labels:  fluent
Fluent-Design-For-Web
Windows 10 Inspired UI For Web
Stars: ✭ 28 (+47.37%)
Mutual labels:  fluent
paginator
Offset pagination for Vapor 🗂
Stars: ✭ 67 (+252.63%)
Mutual labels:  fluent
Finatra
Fast, testable, Scala services built on TwitterServer and Finagle
Stars: ✭ 2,126 (+11089.47%)
Mutual labels:  slf4j
Quick-Pad
Quick Pad is a modern notepad app featuring fluent design
Stars: ✭ 183 (+863.16%)
Mutual labels:  fluent
UniCam
A cross-platform open-source webcam viewer
Stars: ✭ 23 (+21.05%)
Mutual labels:  slf4j
Sofa Common Tools
sofa-common-tools is a library that provide some utility functions to other SOFA libraries.
Stars: ✭ 141 (+642.11%)
Mutual labels:  slf4j
fluent-mysql-driver
🖋🐬 Swift ORM (queries, models, relations, etc) built on MySQL.
Stars: ✭ 69 (+263.16%)
Mutual labels:  fluent
Kotlin Logging
Lightweight logging framework for Kotlin. A convenient and performant logging library wrapping slf4j with Kotlin extensions
Stars: ✭ 1,378 (+7152.63%)
Mutual labels:  slf4j
slf4j-timber
SLF4J binding for Jake Wharton's Timber Android logging library
Stars: ✭ 44 (+131.58%)
Mutual labels:  slf4j
herald
Log annotation for logging frameworks
Stars: ✭ 71 (+273.68%)
Mutual labels:  slf4j
MediaFlyout
Windows 10+ Media Control Taskbar Flyout
Stars: ✭ 87 (+357.89%)
Mutual labels:  fluent
slack-webhook-appender
Logback appender which posts logs to slack via incoming webhook.
Stars: ✭ 16 (-15.79%)
Mutual labels:  slf4j

slf4j-fluent, a fluent API for SLF4J

Build Status License Version security status stability status

slf4j-fluent provides a fluent API for SLF4J.

How to use it

Add slf4j-fluent as a dependency to your project

<dependency> 
  <groupId>org.slf4j</groupId>
  <artifactId>slf4j-log4j12</artifactId>
  <version>1.7.36</version>
</dependency>
<dependency> 
  <groupId>org.fissore</groupId>
  <artifactId>slf4j-fluent</artifactId>
  <version>0.14.0</version>
</dependency>

Initialize FluentLogger and start logging

FluentLogger log = FluentLoggerFactory.getLogger(getClass());

log.debug().log("debug entry with {} args: {}, {}", 2, "value 1", lazy(() -> someObject.expensiveMethod()));

// will add the stacktrace of the cause to the log entry
log.error().withCause(exception).log("An error occured while fetching user {}", user.getId());

// will log every 5 calls to `log` method, instead of every single time
log.error().every(5).log("Errors occured, but we print only one entry every 5");

// will log every 1 second, instead of every single time
log.error().every(1, ChronoUnit.SECONDS).log("Errors occured, but we print only one entry every 1 second");

More examples

import static org.fissore.slf4j.Util.lazy;
[...]
FluentLogger log = FluentLoggerFactory.getLogger(getClass());

// simple log
log.debug().log("debug with no args");

// log with args
String hello = "Hello world";
log.info().log("info with normal arg: {}", hello);

// log with lazy args
String norm = "norm";
log.error().log("error with normal arg: {}, and lazy arg {}", norm, lazy(() -> "lazy arg which takes a while to compute"));

// log with lazy message (available since 0.13.0)
log.warn().log(() -> "a lazy warning message");

// log with cause
Exception e = new Exception();
log.error().withCause(e).log("An error occured");

// rate limiting: log at most every 500 millis
log.error().every(500, ChronoUnit.MILLIS).log("This error will be logged at most every 500 millis");

// rate limiting: log at most every 5 calls
log.error().every(5).log("This error will be logged every 5 times the `log` method is called");

// all together
log.error().withCause(new Exception()).every(10).log(() -> "error with normal arg: {}, and lazy arg {}", norm, lazy(() -> "lazy arg which takes a while to compute"));

Motivation

As slf4j users, we are used to write code like the following:

Logger log = LoggerFactory.getLogger(getClass());

log.debug("debug entry with {} args: {}, {}", 2, "value 1", someObject.expensiveMethod());

This code has 2 problems:

  • the debug method will always be called regardless of the logger level
  • and all the arguments will be evaluated and passed, even that expensiveMethod we'd like not to call unless logger level is really set to debug.

Current solution is to wrap that code this way:

if (log.isDebugEnabled()) {
    log.debug("debug entry with {} args: {}, {}", 2, "value 1", someObject.expensiveMethod());
}

A fluent solution

By using slf4j-fluent together with slf4j, we can rewrite that code this way:

FluentLogger log = FluentLoggerFactory.getLogger(getClass());

log.debug().log("debug entry with {} args: {}, {}", 2, "value 1", lazy(() -> someObject.expensiveMethod()));

The debug() (and error(), info(), etc) method returns a no-op logger when the logger is not set at the appropriate level (which might lead Hotspot to optimize that method call).

The lazy(...) syntax leverages lambdas to postpone argument evaluation to the latest moment.

The log() method has overloads with up to 5 arguments, so the cost of varargs is postponed. If 5 is not enough, open an issue and we'll add more.

Requirements

slf4j-fluent requires java 8 as it uses lambdas.

It is not an slf4j replacement, nor yet-another-logging-framework: it's just a fluent API for slf4j. Which means you can start using it right now with no changes to your existing code.

Trivia

The fluent API looks a lot like that of Flogger, which however has the downside of being yet-another-logging-framework.

Changelog

See Changelog.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].