public final class Logger extends Object
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.
Modifier and Type | Method and Description |
---|---|
static void |
debug(Object source,
String msg)
Protocol one message, with
DEBUG priority level. |
static void |
debug(Object source,
String msg,
Object... args)
Protocol one message, with
DEBUG priority level. |
static void |
error(Object source,
String msg)
Protocol one message, with
ERROR priority level. |
static void |
error(Object source,
String msg,
Object... args)
Protocol one message, with
ERROR priority level. |
static String |
format(String fmt,
Object... args)
Format one string.
|
static void |
info(Object source,
String msg)
Protocol one message, with
INFO priority level. |
static void |
info(Object source,
String msg,
Object... args)
Protocol one message, with
INFO priority level. |
static boolean |
isDebugEnabled(Object source)
Validates whether
DEBUG 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 whether
INFO priority level is enabled for
this particular logger. |
static boolean |
isTraceEnabled(Object source)
Validates whether
TRACE priority level is enabled for
this particular logger. |
static boolean |
isWarnEnabled(Object source)
Validates whether
INFO 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 an
OutputStream , which converts all incoming data
into logging lines (separated by \x0A in UTF-8). |
static void |
trace(Object source,
String msg)
Protocol one message, with
TRACE priority level. |
static void |
trace(Object source,
String msg,
Object... args)
Protocol one message, with
TRACE priority level. |
static void |
warn(Object source,
String msg)
Protocol one message, with
WARN priority level. |
static void |
warn(Object source,
String msg,
Object... args)
Protocol one message, with
WARN priority level. |
public static String format(String fmt, Object... args)
fmt
- The formatargs
- List of arbitrary argumentspublic static void trace(Object source, String msg)
TRACE
priority level.source
- The source of the logging operationmsg
- The text message to be loggedpublic static void trace(Object source, String msg, Object... args)
TRACE
priority level.source
- The source of the logging operationmsg
- The text message to be logged, with meta-tagsargs
- List of argumentspublic static void debug(Object source, String msg)
DEBUG
priority level.source
- The source of the logging operationmsg
- The text message to be logged, with meta-tagspublic static void debug(Object source, String msg, Object... args)
DEBUG
priority level.source
- The source of the logging operationmsg
- The text message to be logged, with meta-tagsargs
- List of argumentspublic static void info(Object source, String msg)
INFO
priority level.source
- The source of the logging operationmsg
- The text message to be loggedpublic static void info(Object source, String msg, Object... args)
INFO
priority level.source
- The source of the logging operationmsg
- The text message to be logged, with meta-tagsargs
- List of argumentspublic static void warn(Object source, String msg)
WARN
priority level.source
- The source of the logging operationmsg
- The text message to be loggedpublic static void warn(Object source, String msg, Object... args)
WARN
priority level.source
- The source of the logging operationmsg
- The text message to be logged, with meta-tagsargs
- List of argumentspublic static void error(Object source, String msg)
ERROR
priority level.source
- The source of the logging operationmsg
- The text message to be loggedpublic static void error(Object source, String msg, Object... args)
ERROR
priority level.source
- The source of the logging operationmsg
- The text message to be logged, with meta-tagsargs
- List of argumentspublic static boolean isTraceEnabled(Object source)
TRACE
priority level is enabled for
this particular logger.source
- The source of the logging operationpublic static boolean isDebugEnabled(Object source)
DEBUG
priority level is enabled for
this particular logger.source
- The source of the logging operationpublic static boolean isInfoEnabled(Object source)
INFO
priority level is enabled for
this particular logger.source
- The source of the logging operationpublic static boolean isWarnEnabled(Object source)
INFO
priority level is enabled for
this particular logger.source
- The source of the logging operationpublic static boolean isEnabled(Level level, Object source)
level
- The level of loggingsource
- The source of the logging operationpublic static void log(Level level, Object source, String msg, Object... args)
level
- The level of loggingsource
- The source of the logging operationmsg
- The text message to be loggedargs
- Optional arguments for string formattingpublic static OutputStream stream(Level level, Object source)
OutputStream
, which converts all incoming data
into logging lines (separated by \x0A
in UTF-8).level
- The level of loggingsource
- The source of the logging operationCopyright © 2012–2016 jcabi.com. All rights reserved.