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.xmlof your project (or in classpath directly).For performance reasons in most cases before sending a
TRACEorDEBUGlog 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
Loggerclass.- Since:
- 0.1
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static voiddebug(Object source, String msg)Protocol one message, withDEBUGpriority level.static voiddebug(Object source, String msg, Object... args)Protocol one message, withDEBUGpriority level.static voiddebugForced(Object source, String msg, Object... args)Protocol one message, withDEBUGpriority level without internal checking whetherDEBUGlevel is enabled.static voiderror(Object source, String msg)Protocol one message, withERRORpriority level.static voiderror(Object source, String msg, Object... args)Protocol one message, withERRORpriority level.static Stringformat(String fmt, Object... args)Format one string.static voidinfo(Object source, String msg)Protocol one message, withINFOpriority level.static voidinfo(Object source, String msg, Object... args)Protocol 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 booleanisEnabled(Level level, Object source)Is 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(Level level, Object source, String msg, Object... args)Log one line using the logging level specified.static OutputStreamstream(Level level, Object source)Returns anOutputStream, which converts all incoming data into logging lines (separated by\x0Ain UTF-8).static voidtrace(Object source, String msg)Protocol one message, withTRACEpriority level.static voidtrace(Object source, String msg, Object... args)Protocol one message, withTRACEpriority level.static voidtraceForced(Object source, String msg, Object... args)Protocol one message, withTRACEpriority level without internal checking whetherTRACElevel is enabled.static voidwarn(Object source, String msg)Protocol one message, withWARNpriority level.static voidwarn(Object source, String msg, Object... args)Protocol 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.SupplierLoggerwithSupplier()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, withTRACEpriority 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, withTRACEpriority 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, 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
public static void debug(Object source, String msg)
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
public static void debug(Object source, String msg, Object... args)
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
public static void debugForced(Object source, String msg, Object... args)
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
public static void info(Object source, String msg)
Protocol one message, withINFOpriority 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, withINFOpriority 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, 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
public static void warn(Object source, String msg)
Protocol one message, withWARNpriority 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, withWARNpriority 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, 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
public static void error(Object source, String msg)
Protocol one message, withERRORpriority 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, withERRORpriority 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 whetherTRACEpriority 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 whetherDEBUGpriority 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 whetherINFOpriority 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 whetherINFOpriority 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\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:
- 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
-
-