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 void
Protocol one message, withDEBUG
priority level.static void
Protocol one message, withDEBUG
priority level.static void
debugForced
(Object source, String msg, Object... args) Protocol one message, withDEBUG
priority level without internal checking whetherDEBUG
level is enabled.static void
Protocol one message, withERROR
priority level.static void
Protocol one message, withERROR
priority level.static String
Format one string.static void
Protocol one message, withINFO
priority level.static void
Protocol one message, withINFO
priority level.static void
infoForced
(Object source, String msg, Object... args) Protocol one message, withINFO
priority level without internal checking whetherINFO
level is enabled.static boolean
isDebugEnabled
(Object source) Validates whetherDEBUG
priority level is enabled for this particular logger.static boolean
Is the given logging level enabled?static boolean
isInfoEnabled
(Object source) Validates whetherINFO
priority level is enabled for this particular logger.static boolean
isTraceEnabled
(Object source) Validates whetherTRACE
priority level is enabled for this particular logger.static boolean
isWarnEnabled
(Object source) Validates whetherINFO
priority level is enabled for this particular logger.static void
Log one line using the logging level specified.static OutputStream
Returns anOutputStream
, which converts all incoming data into logging lines (separated by\x0A
in UTF-8).static void
Protocol one message, withTRACE
priority level.static void
Protocol one message, withTRACE
priority level.static void
traceForced
(Object source, String msg, Object... args) Protocol one message, withTRACE
priority level without internal checking whetherTRACE
level is enabled.static void
Protocol one message, withWARN
priority level.static void
Protocol one message, withWARN
priority level.static void
warnForced
(Object source, String msg, Object... args) Protocol one message, withWARN
priority level without internal checking whetherWARN
level is enabled.static com.jcabi.log.SupplierLogger
Log 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, withTRACE
priority level.- Parameters:
source
- The source of the logging operationmsg
- The text message to be logged- Since:
- 0.7.11
-
trace
Protocol one message, withTRACE
priority 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, withTRACE
priority level without internal checking whetherTRACE
level 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, withDEBUG
priority 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, withDEBUG
priority 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, withDEBUG
priority level without internal checking whetherDEBUG
level 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, withINFO
priority level.- Parameters:
source
- The source of the logging operationmsg
- The text message to be logged- Since:
- 0.7.11
-
info
Protocol one message, withINFO
priority 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, withINFO
priority level without internal checking whetherINFO
level 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, withWARN
priority level.- Parameters:
source
- The source of the logging operationmsg
- The text message to be logged- Since:
- 0.7.11
-
warn
Protocol one message, withWARN
priority 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, withWARN
priority level without internal checking whetherWARN
level 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, withERROR
priority level.- Parameters:
source
- The source of the logging operationmsg
- The text message to be logged- Since:
- 0.7.11
-
error
Protocol one message, withERROR
priority level.- Parameters:
source
- The source of the logging operationmsg
- The text message to be logged, with meta-tagsargs
- List of arguments
-
isTraceEnabled
Validates whetherTRACE
priority level is enabled for this particular logger.- Parameters:
source
- The source of the logging operation- Returns:
- Is it enabled?
-
isDebugEnabled
Validates whetherDEBUG
priority level is enabled for this particular logger.- Parameters:
source
- The source of the logging operation- Returns:
- Is it enabled?
-
isInfoEnabled
Validates whetherINFO
priority level is enabled for this particular logger.- Parameters:
source
- The source of the logging operation- Returns:
- Is it enabled?
- Since:
- 0.5
-
isWarnEnabled
Validates whetherINFO
priority 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\x0A
in 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
-