Making: github.com/elvishew/xL…

Simple, beautiful, powerful, extensible Logging library for Android and Java that can print logs in multiple channels at the same time, such as Logcat, System.out, and files. You can even print to a remote server (or anywhere else) if you want.

What XLog does:

  • Global configuration (TAG, various formatters…) Or based on a single log
  • Support array
  • Unlimited log length support (no 4K character limit)
  • XML and JSON formatted output
  • Thread information (thread name, etc., can be customized)
  • Call stack information (configurable call stack depth, including class name, method name file name, and line number)
  • Save log files (file names and automatic backup policies can be flexibly configured)
  • The logging style in Android Studio is beautiful
  • Easy to use, high scalability

Different from other logging libraries:

  • Beautiful source code, good documentation
  • High scalability, easy to expand and strengthen the function

Rely on

The compile 'com. Elvishew: xlog: 1.0.0'Copy the code

preview

  • Logs with thread information, call stack information, and borders




    classic_log.png

  • The log file




    log_files.png

architecture




architecture.png

usage

Initialize the

A simple way

XLog.init(LogLevel.ALL);Copy the code

Or if you want to disable logging in the official version

XLog.init(BuildConfig.DEBUG ? LogLevel.ALL : LogLevel.NONE);Copy the code

Advanced way

LogConfiguration config = new logconfiguration.builder ().tag("MY_TAG") // Specify the tag, default is "x-log". By default.st(2) // Allows printing of call stack information with depth of 2, by default.b() // Allows printing of log borders, by default.jsonFormatter(new MyJsonFormatter()) // Specifies JSON formatter, Defaultjsonformatter.xmlformatter (new MyXmlFormatter())) // Specifies the XML formatter, DefaultXmlFormatter. ThrowableFormatter(new MyThrowableFormatter())) . The default is DefaultThrowableFormatter threadFormatter (new MyThreadFormatter ()) / / thread information specified formatter, Defaultthreadformatter.stacktraceformatter (new MyStackTraceFormatter())) // Specifies the call stack information formatter, . The default is DefaultStackTraceFormatter borderFormatter (new MyBoardFormatter ()) / / specified border formatter, defaults to DefaultBorderFormatter. The build ();  Printer androidPrinter = new AndroidPrinter(); // SystemPrinter = new SystemPrinter(); // SystemPrinter = new SystemPrinter(); FilePrinter = new filePrinter // print log to filePrinter.Builder("/sdcard/xlog/") // FileNameGenerator(new DateFileNameGenerator()) // Specify the log name generator, Defaults to ChangelessFileNameGenerator (" log "). BackupStrategy (new MyBackupStrategy ()) / / the specified log file backup strategy, The default is FileSizeBackupStrategy(1024 * 1024). LogFlattener(new MyLogFlattener()) // specify a logFlattener, default is defaultloger.build ();  Xlog.init (loglevel. ALL, // specify the LogLevel. Logs below this level will not be printed config, // specify the log configuration, if not specified, New logConfiguration.builder ().build() androidPrinter will be used by default, // add any number of printers. AndroidPrinter systemPrinter, filePrinter will be used by default if no printer is added.Copy the code

For Android, the best place to do initialization is application.onCreate ().

Global usage

XLog.d("Simple message") XLog.d("My name is %s", "Elvis"); XLog.d("An exception caught", exception); XLog.d(array); XLog.json(unformattedJsonString); XLog.xml(unformattedXmlString); . // Other global usesCopy the code

Local usage

Create a Logger.

Logger partial = XLog.tag("PARTIAL-LOG") ... Build ();Copy the code

This Logger is then used locally, and all the methods used to print logs are identical to those used in the XLog class.

partial.d("Simple message 1"); partial.d("Simple message 2"); . // Other local useCopy the code

Usage based on a single log

Do a single log-based configuration, and then you can print the log directly, using all the same methods as in the XLog class.

Xlog.t () // Allows to print thread information. St (3) // Allows to print call stack information with depth of 3. D ("Simple message 1"); Xlog.tag (" temp-tag ").st(0) xlog.tag (" temp-tag ").st(0) D ("Simple message 2"); Xlog.nt () // disables printing thread information.nst () // disables printing call stack information.d ("Simple message 3"); XLog.b().d("Simple message 4");Copy the code

To compare

Let’s imagine a JSON string and an XML string:

String jsonString = "{\"name\": \"Elvis\", \"age\": 18}";
String xmlString = "Elvis"/>Leon"/>";Copy the code

Android Log

Log.d(TAG, "Message");
Log.d(TAG, String.format("Message with argument: age=%s", 18));
Log.d(TAG, jsonString);
Log.d(TAG, xmlString);
Log.d(TAG, "Message with stack trace info", new Throwable());Copy the code



comparison-android-log.png

XLog

XLog.init(LogLevel.ALL);
XLog.d("Message");
XLog.d("Message with argument: age=%s", 18);
XLog.json(jsonString);
XLog.xml(xmlString);
XLog.st(5).d("Message with stack trace info");Copy the code



comparison-xlog.png

XLog with borders

XLog.init(LogLevel.ALL, new LogConfiguration.Builder().b().build());
XLog.d("Message");
XLog.d("Message with argument: age=%s", 18);
XLog.json(jsonString);
XLog.xml(xmlString);
XLog.st(5).d("Message with stack trace info");Copy the code



comparison-xlog-with-border.png

compatibility

To be compatible with Android Log, XLog supports all the methods in Android Log. Look at the Log class in XLog.

Log.v(String, String);
Log.v(String, String, Throwable);
Log.d(String, String);
Log.d(String, String, Throwable);
Log.i(String, String);
Log.i(String, String, Throwable);
Log.w(String, String);
Log.w(String, String, Throwable);
Log.wtf(String, String);
Log.wtf(String, String, Throwable);
Log.e(String, String);
Log.e(String, String, Throwable);
Log.println(int, String, String);
Log.isLoggable(String, int);
Log.getStackTraceString(Throwable);Copy the code

Making: github.com/elvishew/xL…