Class Logger
- java.lang.Object
-
- com.jcabi.log.Logger
-
public final class Logger extends Object
Universal logger, and adapter between your app and SLF4J API.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
orDEBUG
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
All Methods Static Methods Concrete Methods Modifier and Type Method Description static void
debug(Object source, String msg)
Protocol one message, withDEBUG
priority level.static void
debug(Object source, String msg, Object... args)
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
error(Object source, String msg)
Protocol one message, withERROR
priority level.static void
error(Object source, String msg, Object... args)
Protocol one message, withERROR
priority level.static String
format(String fmt, Object... args)
Format one string.static void
info(Object source, String msg)
Protocol one message, withINFO
priority level.static void
info(Object source, String msg, Object... args)
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
isEnabled(Level level, Object source)
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(Level level, Object source, String msg, Object... args)
Log one line using the logging level specified.static OutputStream
stream(Level level, Object source)
Returns anOutputStream
, which converts all incoming data into logging lines (separated by\x0A
in UTF-8).static void
trace(Object source, String msg)
Protocol one message, withTRACE
priority level.static void
trace(Object source, String msg, Object... args)
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
warn(Object source, String msg)
Protocol one message, withWARN
priority level.static void
warn(Object source, String msg, Object... args)
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
withSupplier()
Log messages constructed from Suppliers.
-
-
-
Method Detail
-
format
public static String format(String fmt, Object... args)
Format one string.- Parameters:
fmt
- The formatargs
- List of arbitrary arguments- Returns:
- Formatted string
-
trace
public static void trace(Object source, String msg)
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
public static void trace(Object source, String msg, Object... args)
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
public static void traceForced(Object source, String msg, Object... args)
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
public static void debug(Object source, String msg)
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
public static void debug(Object source, String msg, Object... args)
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
public static void debugForced(Object source, String msg, Object... args)
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
public static void info(Object source, String msg)
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
public static void info(Object source, String msg, Object... args)
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
public static void infoForced(Object source, String msg, Object... args)
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
public static void warn(Object source, String msg)
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
public static void warn(Object source, String msg, Object... args)
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
public static void warnForced(Object source, String msg, Object... args)
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
public static void error(Object source, String msg)
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
public static void error(Object source, String msg, Object... args)
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
public static boolean isTraceEnabled(Object source)
Validates whetherTRACE
priority level is enabled for this particular logger.- Parameters:
source
- The source of the logging operation- Returns:
- Is it enabled?
-
isDebugEnabled
public static boolean isDebugEnabled(Object source)
Validates whetherDEBUG
priority level is enabled for this particular logger.- Parameters:
source
- The source of the logging operation- Returns:
- Is it enabled?
-
isInfoEnabled
public static boolean isInfoEnabled(Object source)
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
public static boolean isWarnEnabled(Object source)
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
public static boolean isEnabled(Level level, Object source)
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
public static void log(Level level, Object source, String msg, Object... args)
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
public static OutputStream stream(Level level, Object source)
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:
- some discussion
- Suppressed Checkstyle violations:
- MagicNumberCheck (20 lines)
-
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
-
-