Class Logger
Instead of relying on some logging engine you can use this class, which transforms all messages to SLF4J. This approach gives you a perfect decoupling of business logic and logging mechanism. All methods in the class are called statically, without the necessity to instantiate the class.
Use it like this in any class, and in any package:
package com.example.XXX;
import com.jcabi.log.Logger;
public class MyClass {
public void foo(Integer num) {
Logger.info(this, "foo(%d) just called", num);
}
}
Or statically (pay attention to MyClass.class):
public class MyClass {
public static void foo(Integer num) {
Logger.info(MyClass.class, "foo(%d) just called", num);
}
}
Exact binding between SLF4J and logging facility has to be
specified in pom.xml of your project (or in classpath directly).
For performance reasons in most cases before sending a
TRACE or DEBUG log message you may check whether this
logging level is enabled in the project, e.g.:
//...
if (Logger.isTraceEnabled(this)) {
Logger.trace(this, "#foo() called");
}
//...
There is only one reason to do so - if you want to save time spent on
preparing of the arguments. By default, such a call is made inside every
method of Logger class.
- Since:
- 0.1
-
Method Summary
Modifier and TypeMethodDescriptionstatic voidProtocol one message, withDEBUGpriority level.static voidProtocol one message, withDEBUGpriority level.static voiddebugForced(Object source, String msg, Object... args) Protocol one message, withDEBUGpriority level without internal checking whetherDEBUGlevel is enabled.static voidProtocol one message, withERRORpriority level.static voidProtocol one message, withERRORpriority level.static StringFormat one string.static voidProtocol one message, withINFOpriority level.static voidProtocol one message, withINFOpriority level.static voidinfoForced(Object source, String msg, Object... args) Protocol one message, withINFOpriority level without internal checking whetherINFOlevel is enabled.static booleanisDebugEnabled(Object source) Validates whetherDEBUGpriority level is enabled for this particular logger.static booleanIs the given logging level enabled?static booleanisInfoEnabled(Object source) Validates whetherINFOpriority level is enabled for this particular logger.static booleanisTraceEnabled(Object source) Validates whetherTRACEpriority level is enabled for this particular logger.static booleanisWarnEnabled(Object source) Validates whetherINFOpriority level is enabled for this particular logger.static voidLog one line using the logging level specified.static OutputStreamReturns anOutputStream, which converts all incoming data into logging lines (separated by\x0Ain UTF-8).static voidProtocol one message, withTRACEpriority level.static voidProtocol one message, withTRACEpriority level.static voidtraceForced(Object source, String msg, Object... args) Protocol one message, withTRACEpriority level without internal checking whetherTRACElevel is enabled.static voidProtocol one message, withWARNpriority level.static voidProtocol one message, withWARNpriority level.static voidwarnForced(Object source, String msg, Object... args) Protocol one message, withWARNpriority level without internal checking whetherWARNlevel is enabled.static com.jcabi.log.SupplierLoggerLog messages constructed from Suppliers.
-
Method Details
-
format
Format one string.- Parameters:
fmt- The formatargs- List of arbitrary arguments- Returns:
- Formatted string
-
trace
Protocol one message, withTRACEpriority level.- Parameters:
source- The source of the logging operationmsg- The text message to be logged- Since:
- 0.7.11
-
trace
Protocol one message, withTRACEpriority level.- Parameters:
source- The source of the logging operationmsg- The text message to be logged, with meta-tagsargs- List of arguments
-
traceForced
Protocol one message, withTRACEpriority level without internal checking whetherTRACElevel is enabled.- Parameters:
source- The source of the logging operationmsg- The text message to be logged, with meta-tagsargs- List of arguments
-
debug
Protocol one message, withDEBUGpriority level.- Parameters:
source- The source of the logging operationmsg- The text message to be logged, with meta-tags- Since:
- 0.7.11
-
debug
Protocol one message, withDEBUGpriority level.- Parameters:
source- The source of the logging operationmsg- The text message to be logged, with meta-tagsargs- List of arguments
-
debugForced
Protocol one message, withDEBUGpriority level without internal checking whetherDEBUGlevel is enabled.- Parameters:
source- The source of the logging operationmsg- The text message to be logged, with meta-tagsargs- List of arguments
-
info
Protocol one message, withINFOpriority level.- Parameters:
source- The source of the logging operationmsg- The text message to be logged- Since:
- 0.7.11
-
info
Protocol one message, withINFOpriority level.- Parameters:
source- The source of the logging operationmsg- The text message to be logged, with meta-tagsargs- List of arguments
-
infoForced
Protocol one message, withINFOpriority level without internal checking whetherINFOlevel is enabled.- Parameters:
source- The source of the logging operationmsg- The text message to be logged, with meta-tagsargs- List of arguments
-
warn
Protocol one message, withWARNpriority level.- Parameters:
source- The source of the logging operationmsg- The text message to be logged- Since:
- 0.7.11
-
warn
Protocol one message, withWARNpriority level.- Parameters:
source- The source of the logging operationmsg- The text message to be logged, with meta-tagsargs- List of arguments
-
warnForced
Protocol one message, withWARNpriority level without internal checking whetherWARNlevel is enabled.- Parameters:
source- The source of the logging operationmsg- The text message to be logged, with meta-tagsargs- List of arguments
-
error
Protocol one message, withERRORpriority level.- Parameters:
source- The source of the logging operationmsg- The text message to be logged- Since:
- 0.7.11
-
error
Protocol one message, withERRORpriority level.- Parameters:
source- The source of the logging operationmsg- The text message to be logged, with meta-tagsargs- List of arguments
-
isTraceEnabled
Validates whetherTRACEpriority level is enabled for this particular logger.- Parameters:
source- The source of the logging operation- Returns:
- Is it enabled?
-
isDebugEnabled
Validates whetherDEBUGpriority level is enabled for this particular logger.- Parameters:
source- The source of the logging operation- Returns:
- Is it enabled?
-
isInfoEnabled
Validates whetherINFOpriority level is enabled for this particular logger.- Parameters:
source- The source of the logging operation- Returns:
- Is it enabled?
- Since:
- 0.5
-
isWarnEnabled
Validates whetherINFOpriority level is enabled for this particular logger.- Parameters:
source- The source of the logging operation- Returns:
- Is it enabled?
- Since:
- 0.5
-
isEnabled
Is the given logging level enabled?- Parameters:
level- The level of loggingsource- The source of the logging operation- Returns:
- Is it enabled?
- Since:
- 0.13
-
log
Log one line using the logging level specified.- Parameters:
level- The level of loggingsource- The source of the logging operationmsg- The text message to be loggedargs- Optional arguments for string formatting- Since:
- 0.8
- Suppressed Checkstyle violations:
- ParameterNumber (4 lines)
-
stream
Returns anOutputStream, which converts all incoming data into logging lines (separated by\x0Ain UTF-8).- Parameters:
level- The level of loggingsource- The source of the logging operation- Returns:
- Output stream directly pointed to the logging facility
- Since:
- 0.8
- See Also:
-
withSupplier
public static com.jcabi.log.SupplierLogger withSupplier()Log messages constructed from Suppliers. It is more efficient to use method referencing because the method won't be called unless the specified logging level is enabled. This saves you the effort of having to check if the level is enabled before calling the logging method. E.g.if (Logger.isDebugEnabled(this)) { Logger.debug(this, "Some %s", calculate()); }
turns into
Logger.withSupplier().debug(this, "Some %s", this::calculate());
and the calculate() method won't be called unless the debug level is active.- Returns:
- Object containing methods for logging with Supplier-constructed messages
-