Top Related Projects
A logger with a small, extensible API which provides utility on top of Android's normal Log class.
Simple Logging Facade for Java
Apache Log4j 2 is a versatile, feature-rich, efficient logging API and backend for Java.
Blazing fast, structured, leveled logging in Go.
Structured, pluggable logging for Go.
Quick Overview
Flogger is a fluent logging API for Java developed by Google. It aims to provide a more readable and flexible logging experience compared to traditional logging frameworks, with a focus on performance and ease of use.
Pros
- Fluent API design for improved readability and ease of use
- High performance with minimal overhead
- Extensive customization options for log formatting and output
- Strong support for structured logging
Cons
- Requires a backend implementation (e.g., java.util.logging or Log4j)
- May require some learning curve for developers used to traditional logging APIs
- Limited adoption outside of Google projects compared to more established logging frameworks
Code Examples
- Basic logging:
import com.google.common.flogger.FluentLogger;
private static final FluentLogger logger = FluentLogger.forEnclosingClass();
public void exampleMethod() {
logger.atInfo().log("This is a simple log message");
}
- Logging with parameters:
public void logWithParameters(String name, int age) {
logger.atInfo()
.with("name", name)
.with("age", age)
.log("User details: %s is %d years old", name, age);
}
- Conditional logging:
public void conditionalLogging(boolean condition) {
logger.atFine()
.whenTrue(condition)
.log("This message will only be logged if the condition is true");
}
- Lazy evaluation:
public void lazyEvaluation(ExpensiveObject obj) {
logger.atFine()
.lazy(() -> "Expensive operation result: " + obj.expensiveOperation())
.log();
}
Getting Started
To use Flogger in your Java project, follow these steps:
- Add Flogger dependencies to your project's build file (e.g., Maven):
<dependencies>
<dependency>
<groupId>com.google.flogger</groupId>
<artifactId>flogger</artifactId>
<version>0.7.4</version>
</dependency>
<dependency>
<groupId>com.google.flogger</groupId>
<artifactId>flogger-system-backend</artifactId>
<version>0.7.4</version>
<scope>runtime</scope>
</dependency>
</dependencies>
- Create a logger instance in your class:
import com.google.common.flogger.FluentLogger;
public class MyClass {
private static final FluentLogger logger = FluentLogger.forEnclosingClass();
// Use logger in your methods
}
- Start logging using the fluent API:
logger.atInfo().log("Hello, Flogger!");
Competitor Comparisons
A logger with a small, extensible API which provides utility on top of Android's normal Log class.
Pros of Timber
- Lightweight and simple to use, with minimal setup required
- Supports custom log adapters for flexibility in output destinations
- Automatically includes the calling class, method, and line number in log messages
Cons of Timber
- Limited built-in formatting options compared to Flogger
- Lacks some advanced features like lazy evaluation and fluent API
Code Comparison
Timber:
Timber.d("User logged in: %s", username)
Flogger:
logger.atInfo().log("User logged in: %s", username);
Key Differences
- Timber is primarily designed for Android, while Flogger is a more general-purpose Java logging framework
- Flogger offers more advanced features like conditional logging and custom log site injection
- Timber uses a global static instance, whereas Flogger typically requires creating logger instances
Use Cases
- Timber: Ideal for Android projects requiring simple, straightforward logging
- Flogger: Better suited for larger Java applications needing more advanced logging capabilities and fine-grained control
Both libraries aim to provide efficient and easy-to-use logging solutions, but they cater to different ecosystems and complexity levels. The choice between them depends on the specific project requirements and the development environment.
Simple Logging Facade for Java
Pros of SLF4J
- Widely adopted and supported in the Java ecosystem
- Offers a simple, facade-based API for logging
- Provides bindings for various logging implementations
Cons of SLF4J
- Lacks advanced features like structured logging
- Performance can be impacted by string concatenation in log statements
Code Comparison
SLF4J:
Logger logger = LoggerFactory.getLogger(MyClass.class);
logger.info("User {} logged in", username);
Flogger:
private static final FluentLogger logger = FluentLogger.forEnclosingClass();
logger.atInfo().log("User %s logged in", username);
Key Differences
- Flogger uses a fluent API, while SLF4J uses a more traditional method-based approach
- Flogger offers better performance through lazy evaluation of log statements
- SLF4J is more widely used and has broader ecosystem support
- Flogger provides more advanced features like customizable log contexts and structured logging
Both logging frameworks have their strengths, with SLF4J being a solid choice for general-purpose logging and Flogger offering more advanced features and potentially better performance. The choice between them often depends on specific project requirements and ecosystem compatibility.
Apache Log4j 2 is a versatile, feature-rich, efficient logging API and backend for Java.
Pros of Log4j2
- More mature and widely adopted in the Java ecosystem
- Offers a broader range of appenders and layouts
- Supports asynchronous logging for improved performance
Cons of Log4j2
- Configuration can be more complex, especially for beginners
- Larger footprint and potential overhead for simple use cases
Code Comparison
Log4j2:
Logger logger = LogManager.getLogger(MyClass.class);
logger.info("This is an informational message");
logger.error("An error occurred", exception);
Flogger:
private static final FluentLogger logger = FluentLogger.forEnclosingClass();
logger.atInfo().log("This is an informational message");
logger.atSevere().withCause(exception).log("An error occurred");
Key Differences
- Flogger uses a fluent API, which can lead to more readable and flexible logging statements
- Log4j2 provides more out-of-the-box functionality and configuration options
- Flogger is designed to be more efficient in terms of memory usage and performance, especially for disabled log statements
Use Cases
- Log4j2 is well-suited for large-scale enterprise applications with complex logging requirements
- Flogger may be preferred for projects prioritizing performance and clean, concise logging code
Blazing fast, structured, leveled logging in Go.
Pros of zap
- Extremely fast and low-allocation logging
- Flexible and customizable logging levels and fields
- Built-in support for structured logging
Cons of zap
- Steeper learning curve due to more complex API
- Less idiomatic Go syntax compared to Flogger
- Requires more setup and configuration
Code Comparison
Flogger:
logger.atInfo().log("User logged in: %s", username)
logger.atInfo().withLabels("user", username).log("Login successful")
zap:
logger.Info("User logged in", zap.String("username", username))
logger.Info("Login successful", zap.String("user", username))
Both Flogger and zap are powerful logging libraries, but they have different approaches. Flogger offers a more familiar and idiomatic Go syntax, while zap focuses on high performance and low allocations. Flogger's fluent API style may be more intuitive for some developers, whereas zap's structured logging approach provides more flexibility and control over log output. The choice between the two often depends on specific project requirements, performance needs, and developer preferences.
Structured, pluggable logging for Go.
Pros of Logrus
- More mature and widely adopted in the Go community
- Offers a wide range of built-in formatters (JSON, text, etc.)
- Supports hooks for custom logging behaviors
Cons of Logrus
- Less performant compared to some newer logging libraries
- API is not as flexible for custom log levels
Code Comparison
Logrus:
log := logrus.New()
log.WithFields(logrus.Fields{
"animal": "walrus",
}).Info("A walrus appears")
Flogger:
logger.atInfo().with("animal", "walrus").log("A walrus appears");
Key Differences
- Logrus is designed for Go, while Flogger is for Java
- Flogger uses a fluent API style, which can be more readable for complex log statements
- Logrus has a simpler setup process, while Flogger requires more configuration
Performance
Flogger generally offers better performance, especially for high-volume logging scenarios, due to its lazy evaluation of log statements and efficient backend design.
Extensibility
Both libraries offer extensibility, but in different ways:
- Logrus uses hooks for custom behaviors
- Flogger allows for custom log sites and backend implementations
Community and Support
Logrus has a larger community and more third-party integrations due to its longer presence in the Go ecosystem. Flogger, being backed by Google, has strong support but a smaller community footprint.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
Flogger: A Fluent Logging API for Java
What is it?
Flogger is a fluent logging API for Java. It supports a wide variety of features, and has many benefits over existing logging APIs.
Come for more self-documenting log statements:
logger.atInfo().withCause(exception).log("Log message with: %s", argument);
Stay for additional features that help you manage your logging better:
logger.atSevere()
.atMostEvery(30, SECONDS)
.log("Value: %s", lazy(() -> doExpensiveCalculation()));
Benefits
While some users prefer "fluency" as a style, this is not what the argument for Flogger rests on. Flogger offers these key, concrete advantages over other logging APIs:
- Logging at disabled levels is effectively free. Finally, you can add as many fine-grained log statements to your code as you want, without worry.
- Flogger also has very high performance for enabled log statements.
- A fluent API accommodates a variety of present and future features without combinatorial explosion, and without requiring separate logging façades.
- Less reliance on long parameter lists makes it harder to misuse and yields more self-documenting code.
Yet another logging API?
The field of open-source Java logging APIs is already extremely crowded, so why add another?
To paraphrase Douglas Adams "Google's codebase is big. Really big. You just wonât believe how vastly hugely mind-bogglingly big it is". Inevitably this resulted in many different debug logging APIs being used throughout the Java codebase, each with its own benefits and issues. Developers were forced to switch between APIs as they worked on different projects, and differences between APIs caused confusion and bugs.
Flogger is the result of an attempt to create a unified logging API, suitable for the vast majority of Java projects in Google.
For something of this magnitude it would have been preferable to use an existing logging API, rather than creating and maintaining our own. However, the Java Core Libraries Team (i.e. Guava maintainers) concluded that Flogger was not slightly better than the alternatives, but much better.
By switching the majority of Java code in Google to use Flogger, many thousands of bugs have been fixed and the cost to developers of learning new logging APIs as they move through the codebase has been eliminated. Flogger is now the sole recommended Java logging API within Google.
How to use Flogger
1. Add the dependencies on Flogger
All code that uses flogger should depend on
com.google.flogger:flogger:<version>
and
com.google.flogger:flogger-system-backend:<version>
.
Note: the dependency on
flogger-system-backend
is only required to be included when the binary is run. If you have a modularized build, you can include this dependency by the root module that builds your app/binary, and can beruntime
scope.
2. Add an import for FluentLogger
import com.google.common.flogger.FluentLogger;
3. Create a private static final
instance
private static final FluentLogger logger = FluentLogger.forEnclosingClass();
4. Start logging:
logger.atInfo().withCause(exception).log("Log message with: %s", argument);
Log messages can use any of Java's printf format
specifiers;
such as %s
, %d
, %016x
etc.
Note that you may also see code and documentation that references the
GoogleLogger
class. This is a minor variant of the default FluentLogger
designed for use in Google's codebase. The FluentLogger
API is recommended for
non-Google code, since its API should remain more stable over time.
More information
Flogger was designed and implemented by David Beaumont, with invaluable help from the Java Core Libraries Team and many other Googlers.
If you interested in a deeper dive into the rationale behind Flogger's API, please see Anatomy of an API.
Top Related Projects
A logger with a small, extensible API which provides utility on top of Android's normal Log class.
Simple Logging Facade for Java
Apache Log4j 2 is a versatile, feature-rich, efficient logging API and backend for Java.
Blazing fast, structured, leveled logging in Go.
Structured, pluggable logging for Go.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot