Class XLog
- java.lang.Object
-
- com.elvishew.xlog.XLog
-
public class XLog extends java.lang.ObjectA log tool which can be used in android or java, the most important feature is it can print the logs to multiple place in the same time, such as android shell, console and file, you can even print the log to the remote server if you want, all of these can be done just within one calling.
Also, XLog is very flexible, almost every component is replaceable.How to use in a general way:
1. Initial the log system.
Using one ofinit()init(int),init(LogConfiguration)init(Printer...),init(int, Printer...),init(LogConfiguration, Printer...),
that will setup aLoggerfor a global usage. If you want to use a customized configuration instead of the global one to log something, you can start a customization logging.For android, a best place to do the initialization is
Application.onCreate().2. Start to log.
v(String, Object...),v(String)andv(String, Throwable)are for logging aLogLevel.VERBOSEmessage.d(String, Object...),d(String)andd(String, Throwable)are for logging aLogLevel.DEBUGmessage.i(String, Object...),i(String)andi(String, Throwable)are for logging aLogLevel.INFOmessage.w(String, Object...),w(String)andw(String, Throwable)are for logging aLogLevel.WARNmessage.e(String, Object...),e(String)ande(String, Throwable)are for logging aLogLevel.ERRORmessage.log(int, String, Object...),log(int, String)andlog(int, String, Throwable)are for logging a specific level message.json(String)is for logging aLogLevel.DEBUGJSON string.xml(String)is for logging aLogLevel.DEBUGXML string.
Also, you can directly log any object with specific log level, likev(Object), and any object array with specific log level, likev(Object[]).How to use in a dynamically customizing way after initializing the log system:
1. Start a customization.
Call any oflogLevel(int)tag(String),enableThreadInfo(),disableThreadInfo(),enableStackTrace(int),enableStackTrace(String, int),disableStackTrace(),enableBorder(),disableBorder(),jsonFormatter(JsonFormatter),xmlFormatter(XmlFormatter),threadFormatter(ThreadFormatter),stackTraceFormatter(StackTraceFormatter),throwableFormatter(ThrowableFormatter)borderFormatter(BorderFormatter)addObjectFormatter(Class, ObjectFormatter)addInterceptor(Interceptor)printers(Printer...),
it will return aLogger.Builderobject.2. Finish the customization.
Continue to setup other fields of the returnedLogger.Builder.3. Build a dynamically generated
Logger.
Call theLogger.Builder.build()of the returnedLogger.Builder.4. Start to log.
The logging methods of aLoggeris completely same as that ones inXLog.
As a convenience, you can ignore the step 3, just call the logging methods ofLogger.Builder, it will automatically build aLoggerand call the target logging method.Compatibility:
In order to be compatible with
Log, all the methods ofLogare supported here. See:XLog.Log.v(String, String),XLog.Log.v(String, String, Throwable)XLog.Log.d(String, String),XLog.Log.d(String, String, Throwable)XLog.Log.i(String, String),XLog.Log.i(String, String, Throwable)XLog.Log.w(String, String),XLog.Log.w(String, String, Throwable)XLog.Log.wtf(String, String),XLog.Log.wtf(String, String, Throwable)XLog.Log.e(String, String),XLog.Log.e(String, String, Throwable)XLog.Log.println(int, String, String)XLog.Log.isLoggable(String, int)XLog.Log.getStackTraceString(Throwable)
-
-
Method Summary
All Methods Static Methods Concrete Methods Deprecated Methods Modifier and Type Method Description static Logger.BuilderaddInterceptor(Interceptor interceptor)Start to customize aLoggerand add an interceptor.static <T> Logger.BuilderaddObjectFormatter(java.lang.Class<T> objectClass, ObjectFormatter<? super T> objectFormatter)Start to customize aLoggerand add an object formatter for specific class of object.static Logger.Builderb()Deprecated.useenableBorder()instead, since 1.7.0static Logger.BuilderborderFormatter(BorderFormatter borderFormatter)Start to customize aLoggerand set theBorderFormatter.static voidd(java.lang.Object object)Log an object with levelLogLevel.DEBUG.static voidd(java.lang.Object[] array)Log an array with levelLogLevel.DEBUG.static voidd(java.lang.String msg)Log a message with levelLogLevel.DEBUG.static voidd(java.lang.String format, java.lang.Object... args)Log a message with levelLogLevel.DEBUG.static voidd(java.lang.String msg, java.lang.Throwable tr)Log a message and a throwable with levelLogLevel.DEBUG.static Logger.BuilderdisableBorder()Start to customize aLoggerand disable border, the log content won't be surrounded by a border.static Logger.BuilderdisableStackTrace()Start to customize aLoggerand disable stack trace, the stack trace won't be printed with the log message.static Logger.BuilderdisableThreadInfo()Start to customize aLoggerand disable thread info, the thread info won't be printed with the log message.static voide(java.lang.Object object)Log an object with levelLogLevel.ERROR.static voide(java.lang.Object[] array)Log an array with levelLogLevel.ERROR.static voide(java.lang.String msg)Log a message with levelLogLevel.ERROR.static voide(java.lang.String format, java.lang.Object... args)Log a message with levelLogLevel.ERROR.static voide(java.lang.String msg, java.lang.Throwable tr)Log a message and a throwable with levelLogLevel.ERROR.static Logger.BuilderenableBorder()Start to customize aLoggerand enable border, the border would surround the entire log content, and separate the log message, thread info and stack trace.static Logger.BuilderenableStackTrace(int depth)Start to customize aLoggerand enable stack trace, the stack trace would be printed with the log message.static Logger.BuilderenableStackTrace(java.lang.String stackTraceOrigin, int depth)Start to customize aLoggerand enable stack trace, the stack trace would be printed with the log message.static Logger.BuilderenableThreadInfo()Start to customize aLoggerand enable thread info, the thread info would be printed with the log message.static voidi(java.lang.Object object)Log an object with levelLogLevel.INFO.static voidi(java.lang.Object[] array)Log an array with levelLogLevel.INFO.static voidi(java.lang.String msg)Log a message with levelLogLevel.INFO.static voidi(java.lang.String format, java.lang.Object... args)Log a message with levelLogLevel.INFO.static voidi(java.lang.String msg, java.lang.Throwable tr)Log a message and a throwable with levelLogLevel.INFO.static voidinit()Initialize log system, should be called only once.static voidinit(int logLevel)Initialize log system, should be called only once.static voidinit(int logLevel, LogConfiguration logConfiguration)Deprecated.the log level is part of log configuration now, useinit(LogConfiguration)instead, since 1.3.0static voidinit(int logLevel, LogConfiguration logConfiguration, Printer... printers)Deprecated.the log level is part of log configuration now, useinit(LogConfiguration, Printer...)instead, since 1.3.0static voidinit(int logLevel, Printer... printers)Initialize log system, should be called only once.static voidinit(LogConfiguration logConfiguration)Initialize log system, should be called only once.static voidinit(LogConfiguration logConfiguration, Printer... printers)Initialize log system, should be called only once.static voidinit(Printer... printers)Initialize log system, should be called only once.static voidjson(java.lang.String json)Log a JSON string, with levelLogLevel.DEBUGby default.static Logger.BuilderjsonFormatter(JsonFormatter jsonFormatter)Start to customize aLoggerand set theJsonFormatter.static voidlog(int logLevel, java.lang.Object object)Log an object with specific log level.static voidlog(int logLevel, java.lang.Object[] array)Log an array with specific log level.static voidlog(int logLevel, java.lang.String msg)Log a message with specific log level.static voidlog(int logLevel, java.lang.String format, java.lang.Object... args)Log a message with specific log level.static voidlog(int logLevel, java.lang.String msg, java.lang.Throwable tr)Log a message and a throwable with specific log level.static Logger.BuilderlogLevel(int logLevel)Start to customize aLoggerand set the log level.static Logger.Buildernb()Deprecated.usedisableBorder()instead, since 1.7.0static Logger.Buildernst()Deprecated.usedisableStackTrace()instead, since 1.7.0static Logger.Buildernt()Deprecated.usedisableThreadInfo()instead, since 1.7.0static Logger.Builderprinters(Printer... printers)static Logger.Builderst(int depth)Deprecated.useenableStackTrace(int)instead, since 1.7.0static Logger.Builderst(java.lang.String stackTraceOrigin, int depth)Deprecated.useenableStackTrace(String, int)instead, since 1.7.0static Logger.BuilderstackTraceFormatter(StackTraceFormatter stackTraceFormatter)Start to customize aLoggerand set theStackTraceFormatter.static Logger.Buildert()Deprecated.useenableThreadInfo()instead, since 1.7.0static Logger.Buildertag(java.lang.String tag)Start to customize aLoggerand set the tag.static Logger.BuilderthreadFormatter(ThreadFormatter threadFormatter)Start to customize aLoggerand set theThreadFormatter.static Logger.BuilderthrowableFormatter(ThrowableFormatter throwableFormatter)Start to customize aLoggerand set theThrowableFormatter.static voidv(java.lang.Object object)Log an object with levelLogLevel.VERBOSE.static voidv(java.lang.Object[] array)Log an array with levelLogLevel.VERBOSE.static voidv(java.lang.String msg)Log a message with levelLogLevel.VERBOSE.static voidv(java.lang.String format, java.lang.Object... args)Log a message with levelLogLevel.VERBOSE.static voidv(java.lang.String msg, java.lang.Throwable tr)Log a message and a throwable with levelLogLevel.VERBOSE.static voidw(java.lang.Object object)Log an object with levelLogLevel.WARN.static voidw(java.lang.Object[] array)Log an array with levelLogLevel.WARN.static voidw(java.lang.String msg)Log a message with levelLogLevel.WARN.static voidw(java.lang.String format, java.lang.Object... args)Log a message with levelLogLevel.WARN.static voidw(java.lang.String msg, java.lang.Throwable tr)Log a message and a throwable with levelLogLevel.WARN.static voidxml(java.lang.String xml)Log a XML string, with levelLogLevel.DEBUGby default.static Logger.BuilderxmlFormatter(XmlFormatter xmlFormatter)Start to customize aLoggerand set theXmlFormatter.
-
-
-
Method Detail
-
init
public static void init()
Initialize log system, should be called only once.- Since:
- 1.3.0
-
init
public static void init(int logLevel)
Initialize log system, should be called only once.- Parameters:
logLevel- the log level, logs with a lower level than which would not be printed
-
init
@Deprecated public static void init(int logLevel, LogConfiguration logConfiguration)Deprecated.the log level is part of log configuration now, useinit(LogConfiguration)instead, since 1.3.0Initialize log system, should be called only once.- Parameters:
logLevel- the log level, logs with a lower level than which would not be printedlogConfiguration- the log configuration
-
init
public static void init(LogConfiguration logConfiguration)
Initialize log system, should be called only once.- Parameters:
logConfiguration- the log configuration- Since:
- 1.3.0
-
init
public static void init(Printer... printers)
Initialize log system, should be called only once.- Parameters:
printers- the printers, each log would be printed by all of the printers- Since:
- 1.3.0
-
init
public static void init(int logLevel, Printer... printers)Initialize log system, should be called only once.- Parameters:
logLevel- the log level, logs with a lower level than which would not be printedprinters- the printers, each log would be printed by all of the printers
-
init
@Deprecated public static void init(int logLevel, LogConfiguration logConfiguration, Printer... printers)Deprecated.the log level is part of log configuration now, useinit(LogConfiguration, Printer...)instead, since 1.3.0Initialize log system, should be called only once.- Parameters:
logLevel- the log level, logs with a lower level than which would not be printedlogConfiguration- the log configurationprinters- the printers, each log would be printed by all of the printers
-
init
public static void init(LogConfiguration logConfiguration, Printer... printers)
Initialize log system, should be called only once.- Parameters:
logConfiguration- the log configurationprinters- the printers, each log would be printed by all of the printers- Since:
- 1.3.0
-
logLevel
public static Logger.Builder logLevel(int logLevel)
Start to customize aLoggerand set the log level.- Parameters:
logLevel- the log level to customize- Returns:
- the
Logger.Builderto build theLogger - Since:
- 1.3.0
-
tag
public static Logger.Builder tag(java.lang.String tag)
Start to customize aLoggerand set the tag.- Parameters:
tag- the tag to customize- Returns:
- the
Logger.Builderto build theLogger
-
t
@Deprecated public static Logger.Builder t()
Deprecated.useenableThreadInfo()instead, since 1.7.0Start to customize aLoggerand enable thread info.- Returns:
- the
Logger.Builderto build theLogger
-
enableThreadInfo
public static Logger.Builder enableThreadInfo()
Start to customize aLoggerand enable thread info, the thread info would be printed with the log message.- Returns:
- the
Logger.Builderto build theLogger - Since:
- 1.7.0
- See Also:
ThreadFormatter
-
nt
@Deprecated public static Logger.Builder nt()
Deprecated.usedisableThreadInfo()instead, since 1.7.0Start to customize aLoggerand disable thread info.- Returns:
- the
Logger.Builderto build theLogger
-
disableThreadInfo
public static Logger.Builder disableThreadInfo()
Start to customize aLoggerand disable thread info, the thread info won't be printed with the log message.- Returns:
- the
Logger.Builderto build theLogger - Since:
- 1.7.0
-
st
@Deprecated public static Logger.Builder st(int depth)
Deprecated.useenableStackTrace(int)instead, since 1.7.0Start to customize aLoggerand enable stack trace.- Parameters:
depth- the number of stack trace elements we should log, 0 if no limitation- Returns:
- the
Logger.Builderto build theLogger
-
enableStackTrace
public static Logger.Builder enableStackTrace(int depth)
Start to customize aLoggerand enable stack trace, the stack trace would be printed with the log message.- Parameters:
depth- the number of stack trace elements we should log, 0 if no limitation- Returns:
- the
Logger.Builderto build theLogger - Since:
- 1.7.0
- See Also:
StackTraceFormatter
-
st
@Deprecated public static Logger.Builder st(java.lang.String stackTraceOrigin, int depth)
Deprecated.useenableStackTrace(String, int)instead, since 1.7.0Start to customize aLoggerand enable stack trace.- Parameters:
stackTraceOrigin- the origin of stack trace elements from which we should NOT log, it can be a package name like "com.elvishew.xlog", a class name like "com.yourdomain.logWrapper", or something else between package name and class name, like "com.yourdomain.". It is mostly used when you are using a logger wrapperdepth- the number of stack trace elements we should log, 0 if no limitation- Returns:
- the
Logger.Builderto build theLogger - Since:
- 1.4.0
-
enableStackTrace
public static Logger.Builder enableStackTrace(java.lang.String stackTraceOrigin, int depth)
Start to customize aLoggerand enable stack trace, the stack trace would be printed with the log message.- Parameters:
stackTraceOrigin- the origin of stack trace elements from which we should NOT log, it can be a package name like "com.elvishew.xlog", a class name like "com.yourdomain.logWrapper", or something else between package name and class name, like "com.yourdomain.". It is mostly used when you are using a logger wrapperdepth- the number of stack trace elements we should log, 0 if no limitation- Returns:
- the
Logger.Builderto build theLogger - Since:
- 1.7.0
- See Also:
StackTraceFormatter
-
nst
@Deprecated public static Logger.Builder nst()
Deprecated.usedisableStackTrace()instead, since 1.7.0Start to customize aLoggerand disable stack trace.- Returns:
- the
Logger.Builderto build theLogger
-
disableStackTrace
public static Logger.Builder disableStackTrace()
Start to customize aLoggerand disable stack trace, the stack trace won't be printed with the log message.- Returns:
- the
Logger.Builderto build theLogger - Since:
- 1.7.0
-
b
@Deprecated public static Logger.Builder b()
Deprecated.useenableBorder()instead, since 1.7.0Start to customize aLoggerand enable border.- Returns:
- the
Logger.Builderto build theLogger
-
enableBorder
public static Logger.Builder enableBorder()
Start to customize aLoggerand enable border, the border would surround the entire log content, and separate the log message, thread info and stack trace.- Returns:
- the
Logger.Builderto build theLogger - Since:
- 1.7.0
- See Also:
BorderFormatter
-
nb
@Deprecated public static Logger.Builder nb()
Deprecated.usedisableBorder()instead, since 1.7.0Start to customize aLoggerand disable border.- Returns:
- the
Logger.Builderto build theLogger
-
disableBorder
public static Logger.Builder disableBorder()
Start to customize aLoggerand disable border, the log content won't be surrounded by a border.- Returns:
- the
Logger.Builderto build theLogger - Since:
- 1.7.0
-
jsonFormatter
public static Logger.Builder jsonFormatter(JsonFormatter jsonFormatter)
Start to customize aLoggerand set theJsonFormatter.- Parameters:
jsonFormatter- theJsonFormatterto customize- Returns:
- the
Logger.Builderto build theLogger
-
xmlFormatter
public static Logger.Builder xmlFormatter(XmlFormatter xmlFormatter)
Start to customize aLoggerand set theXmlFormatter.- Parameters:
xmlFormatter- theXmlFormatterto customize- Returns:
- the
Logger.Builderto build theLogger
-
throwableFormatter
public static Logger.Builder throwableFormatter(ThrowableFormatter throwableFormatter)
Start to customize aLoggerand set theThrowableFormatter.- Parameters:
throwableFormatter- theThrowableFormatterto customize- Returns:
- the
Logger.Builderto build theLogger
-
threadFormatter
public static Logger.Builder threadFormatter(ThreadFormatter threadFormatter)
Start to customize aLoggerand set theThreadFormatter.- Parameters:
threadFormatter- theThreadFormatterto customize- Returns:
- the
Logger.Builderto build theLogger
-
stackTraceFormatter
public static Logger.Builder stackTraceFormatter(StackTraceFormatter stackTraceFormatter)
Start to customize aLoggerand set theStackTraceFormatter.- Parameters:
stackTraceFormatter- theStackTraceFormatterto customize- Returns:
- the
Logger.Builderto build theLogger
-
borderFormatter
public static Logger.Builder borderFormatter(BorderFormatter borderFormatter)
Start to customize aLoggerand set theBorderFormatter.- Parameters:
borderFormatter- theBorderFormatterto customize- Returns:
- the
Logger.Builderto build theLogger
-
addObjectFormatter
public static <T> Logger.Builder addObjectFormatter(java.lang.Class<T> objectClass, ObjectFormatter<? super T> objectFormatter)
Start to customize aLoggerand add an object formatter for specific class of object.- Type Parameters:
T- the type of object- Parameters:
objectClass- the class of objectobjectFormatter- the object formatter to add- Returns:
- the
Logger.Builderto build theLogger - Since:
- 1.1.0
-
addInterceptor
public static Logger.Builder addInterceptor(Interceptor interceptor)
Start to customize aLoggerand add an interceptor.- Parameters:
interceptor- the interceptor to add- Returns:
- the
Logger.Builderto build theLogger - Since:
- 1.3.0
-
printers
public static Logger.Builder printers(Printer... printers)
- Parameters:
printers- thePrinterarray to customize- Returns:
- the
Logger.Builderto build theLogger
-
v
public static void v(java.lang.Object object)
Log an object with levelLogLevel.VERBOSE.- Parameters:
object- the object to log- Since:
- 1.1.0
- See Also:
LogConfiguration.Builder.addObjectFormatter(Class, ObjectFormatter)
-
v
public static void v(java.lang.Object[] array)
Log an array with levelLogLevel.VERBOSE.- Parameters:
array- the array to log
-
v
public static void v(java.lang.String format, java.lang.Object... args)Log a message with levelLogLevel.VERBOSE.- Parameters:
format- the format of the message to logargs- the arguments of the message to log
-
v
public static void v(java.lang.String msg)
Log a message with levelLogLevel.VERBOSE.- Parameters:
msg- the message to log
-
v
public static void v(java.lang.String msg, java.lang.Throwable tr)Log a message and a throwable with levelLogLevel.VERBOSE.- Parameters:
msg- the message to logtr- the throwable to be log
-
d
public static void d(java.lang.Object object)
Log an object with levelLogLevel.DEBUG.- Parameters:
object- the object to log- Since:
- 1.1.0
- See Also:
LogConfiguration.Builder.addObjectFormatter(Class, ObjectFormatter)
-
d
public static void d(java.lang.Object[] array)
Log an array with levelLogLevel.DEBUG.- Parameters:
array- the array to log
-
d
public static void d(java.lang.String format, java.lang.Object... args)Log a message with levelLogLevel.DEBUG.- Parameters:
format- the format of the message to logargs- the arguments of the message to log
-
d
public static void d(java.lang.String msg)
Log a message with levelLogLevel.DEBUG.- Parameters:
msg- the message to log
-
d
public static void d(java.lang.String msg, java.lang.Throwable tr)Log a message and a throwable with levelLogLevel.DEBUG.- Parameters:
msg- the message to logtr- the throwable to be log
-
i
public static void i(java.lang.Object object)
Log an object with levelLogLevel.INFO.- Parameters:
object- the object to log- Since:
- 1.1.0
- See Also:
LogConfiguration.Builder.addObjectFormatter(Class, ObjectFormatter)
-
i
public static void i(java.lang.Object[] array)
Log an array with levelLogLevel.INFO.- Parameters:
array- the array to log
-
i
public static void i(java.lang.String format, java.lang.Object... args)Log a message with levelLogLevel.INFO.- Parameters:
format- the format of the message to logargs- the arguments of the message to log
-
i
public static void i(java.lang.String msg)
Log a message with levelLogLevel.INFO.- Parameters:
msg- the message to log
-
i
public static void i(java.lang.String msg, java.lang.Throwable tr)Log a message and a throwable with levelLogLevel.INFO.- Parameters:
msg- the message to logtr- the throwable to be log
-
w
public static void w(java.lang.Object object)
Log an object with levelLogLevel.WARN.- Parameters:
object- the object to log- Since:
- 1.1.0
- See Also:
LogConfiguration.Builder.addObjectFormatter(Class, ObjectFormatter)
-
w
public static void w(java.lang.Object[] array)
Log an array with levelLogLevel.WARN.- Parameters:
array- the array to log
-
w
public static void w(java.lang.String format, java.lang.Object... args)Log a message with levelLogLevel.WARN.- Parameters:
format- the format of the message to logargs- the arguments of the message to log
-
w
public static void w(java.lang.String msg)
Log a message with levelLogLevel.WARN.- Parameters:
msg- the message to log
-
w
public static void w(java.lang.String msg, java.lang.Throwable tr)Log a message and a throwable with levelLogLevel.WARN.- Parameters:
msg- the message to logtr- the throwable to be log
-
e
public static void e(java.lang.Object object)
Log an object with levelLogLevel.ERROR.- Parameters:
object- the object to log- Since:
- 1.1.0
- See Also:
LogConfiguration.Builder.addObjectFormatter(Class, ObjectFormatter)
-
e
public static void e(java.lang.Object[] array)
Log an array with levelLogLevel.ERROR.- Parameters:
array- the array to log
-
e
public static void e(java.lang.String format, java.lang.Object... args)Log a message with levelLogLevel.ERROR.- Parameters:
format- the format of the message to logargs- the arguments of the message to log
-
e
public static void e(java.lang.String msg)
Log a message with levelLogLevel.ERROR.- Parameters:
msg- the message to log
-
e
public static void e(java.lang.String msg, java.lang.Throwable tr)Log a message and a throwable with levelLogLevel.ERROR.- Parameters:
msg- the message to logtr- the throwable to be log
-
log
public static void log(int logLevel, java.lang.Object object)Log an object with specific log level.- Parameters:
logLevel- the specific log levelobject- the object to log- Since:
- 1.4.0
- See Also:
LogConfiguration.Builder.addObjectFormatter(Class, ObjectFormatter)
-
log
public static void log(int logLevel, java.lang.Object[] array)Log an array with specific log level.- Parameters:
logLevel- the specific log levelarray- the array to log- Since:
- 1.4.0
-
log
public static void log(int logLevel, java.lang.String format, java.lang.Object... args)Log a message with specific log level.- Parameters:
logLevel- the specific log levelformat- the format of the message to logargs- the arguments of the message to log- Since:
- 1.4.0
-
log
public static void log(int logLevel, java.lang.String msg)Log a message with specific log level.- Parameters:
logLevel- the specific log levelmsg- the message to log- Since:
- 1.4.0
-
log
public static void log(int logLevel, java.lang.String msg, java.lang.Throwable tr)Log a message and a throwable with specific log level.- Parameters:
logLevel- the specific log levelmsg- the message to logtr- the throwable to be log- Since:
- 1.4.0
-
json
public static void json(java.lang.String json)
Log a JSON string, with levelLogLevel.DEBUGby default.- Parameters:
json- the JSON string to log
-
xml
public static void xml(java.lang.String xml)
Log a XML string, with levelLogLevel.DEBUGby default.- Parameters:
xml- the XML string to log
-
-