{"id":18691057,"url":"https://github.com/ffissore/slf4j-fluent","last_synced_at":"2025-04-12T06:18:20.215Z","repository":{"id":34186153,"uuid":"164441048","full_name":"ffissore/slf4j-fluent","owner":"ffissore","description":"A fluent api for SLF4J","archived":false,"fork":false,"pushed_at":"2023-03-06T04:58:36.000Z","size":100,"stargazers_count":19,"open_issues_count":6,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-12T06:18:11.047Z","etag":null,"topics":["fluent","java","logging","slf4j"],"latest_commit_sha":null,"homepage":"","language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ffissore.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-01-07T14:02:58.000Z","updated_at":"2024-03-31T14:21:57.000Z","dependencies_parsed_at":"2024-11-07T10:53:10.040Z","dependency_job_id":"74df9c9c-49d9-4236-b4b6-1f5ae51ccf87","html_url":"https://github.com/ffissore/slf4j-fluent","commit_stats":null,"previous_names":[],"tags_count":15,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ffissore%2Fslf4j-fluent","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ffissore%2Fslf4j-fluent/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ffissore%2Fslf4j-fluent/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ffissore%2Fslf4j-fluent/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ffissore","download_url":"https://codeload.github.com/ffissore/slf4j-fluent/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248525141,"owners_count":21118620,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["fluent","java","logging","slf4j"],"created_at":"2024-11-07T10:53:04.587Z","updated_at":"2025-04-12T06:18:20.169Z","avatar_url":"https://github.com/ffissore.png","language":"Java","funding_links":[],"categories":[],"sub_categories":[],"readme":"# slf4j-fluent, a fluent API for SLF4J\n\n[![Build Status](https://travis-ci.org/ffissore/slf4j-fluent.svg?branch=master)](https://travis-ci.org/ffissore/slf4j-fluent)\n![License](https://img.shields.io/github/license/ffissore/slf4j-fluent.svg)\n![Version](https://img.shields.io/maven-central/v/org.fissore/slf4j-fluent.svg)\n[![security status](https://www.meterian.com/badge/gh/ffissore/slf4j-fluent/security)](https://www.meterian.com/report/gh/ffissore/slf4j-fluent)\n[![stability status](https://www.meterian.com/badge/gh/ffissore/slf4j-fluent/stability)](https://www.meterian.com/report/gh/ffissore/slf4j-fluent)\n\nslf4j-fluent provides a fluent API for [SLF4J](https://www.slf4j.org/).\n\n## How to use it\n\nAdd slf4j-fluent as a dependency to your project\n\n```xml\n\u003cdependency\u003e \n  \u003cgroupId\u003eorg.slf4j\u003c/groupId\u003e\n  \u003cartifactId\u003eslf4j-log4j12\u003c/artifactId\u003e\n  \u003cversion\u003e1.7.36\u003c/version\u003e\n\u003c/dependency\u003e\n\u003cdependency\u003e \n  \u003cgroupId\u003eorg.fissore\u003c/groupId\u003e\n  \u003cartifactId\u003eslf4j-fluent\u003c/artifactId\u003e\n  \u003cversion\u003e0.14.0\u003c/version\u003e\n\u003c/dependency\u003e\n```\n\nInitialize FluentLogger and start logging\n\n```java\nFluentLogger log = FluentLoggerFactory.getLogger(getClass());\n\nlog.debug().log(\"debug entry with {} args: {}, {}\", 2, \"value 1\", lazy(() -\u003e someObject.expensiveMethod()));\n\n// will add the stacktrace of the cause to the log entry\nlog.error().withCause(exception).log(\"An error occured while fetching user {}\", user.getId());\n\n// will log every 5 calls to `log` method, instead of every single time\nlog.error().every(5).log(\"Errors occured, but we print only one entry every 5\");\n\n// will log every 1 second, instead of every single time\nlog.error().every(1, ChronoUnit.SECONDS).log(\"Errors occured, but we print only one entry every 1 second\");\n```\n\n## More examples\n\n```java\nimport static org.fissore.slf4j.Util.lazy;\n[...]\nFluentLogger log = FluentLoggerFactory.getLogger(getClass());\n\n// simple log\nlog.debug().log(\"debug with no args\");\n\n// log with args\nString hello = \"Hello world\";\nlog.info().log(\"info with normal arg: {}\", hello);\n\n// log with lazy args\nString norm = \"norm\";\nlog.error().log(\"error with normal arg: {}, and lazy arg {}\", norm, lazy(() -\u003e \"lazy arg which takes a while to compute\"));\n\n// log with lazy message (available since 0.13.0)\nlog.warn().log(() -\u003e \"a lazy warning message\");\n\n// log with cause\nException e = new Exception();\nlog.error().withCause(e).log(\"An error occured\");\n\n// rate limiting: log at most every 500 millis\nlog.error().every(500, ChronoUnit.MILLIS).log(\"This error will be logged at most every 500 millis\");\n\n// rate limiting: log at most every 5 calls\nlog.error().every(5).log(\"This error will be logged every 5 times the `log` method is called\");\n\n// all together\nlog.error().withCause(new Exception()).every(10).log(() -\u003e \"error with normal arg: {}, and lazy arg {}\", norm, lazy(() -\u003e \"lazy arg which takes a while to compute\"));\n```\n\n## Motivation\n\nAs slf4j users, we are used to write code like the following:\n\n```java\nLogger log = LoggerFactory.getLogger(getClass());\n\nlog.debug(\"debug entry with {} args: {}, {}\", 2, \"value 1\", someObject.expensiveMethod());\n```\n\nThis code has 2 problems:\n\n* the `debug` method will always be called regardless of the logger level\n* 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`.\n\nCurrent solution is to wrap that code this way:\n\n```java\nif (log.isDebugEnabled()) {\n    log.debug(\"debug entry with {} args: {}, {}\", 2, \"value 1\", someObject.expensiveMethod());\n}\n```\n\n## A fluent solution\n\nBy using `slf4j-fluent` together with slf4j, we can rewrite that code this way:\n\n```java\nFluentLogger log = FluentLoggerFactory.getLogger(getClass());\n\nlog.debug().log(\"debug entry with {} args: {}, {}\", 2, \"value 1\", lazy(() -\u003e someObject.expensiveMethod()));\n```\n\nThe `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\ncall).\n\nThe `lazy(...)` syntax leverages lambdas to postpone argument evaluation to the latest moment.\n\nThe `log()` method has overloads with up to 5 arguments, so [the cost of varargs](https://stackoverflow.com/questions/2426455/javas-varargs-performance) is postponed. If 5 is not\nenough, open an issue and we'll add more.\n\n## Requirements\n\nslf4j-fluent requires java 8 as it uses lambdas.\n\nIt 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\ncode.\n\n## Trivia\n\nThe fluent API looks a lot like that of [Flogger](https://github.com/google/flogger), which however has the downside of being yet-another-logging-framework.\n\n## Changelog\n\nSee [Changelog](CHANGELOG.md).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fffissore%2Fslf4j-fluent","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fffissore%2Fslf4j-fluent","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fffissore%2Fslf4j-fluent/lists"}