Log directory full path
xLog is a lightweight, beautiful, and extensible logging library for Android and Java that can simultaneously print logs in files such as Logcat, Console, and files.
Peculiarity:
Global configuration (TAG, various formatters…) or a single-log based configuration
Support for printing arbitrary objects as well as customizable object formatters
Support for printing unlimited logs (no 4K character limit)
XML and JSON formatted output
How to Use:
1. Add dependencies
implementation ‘com.elvishew:xlog:1.10.1’
2. Initialize the configuration
When initialized, it can be done in the simplest way
XLog.init(LogLevel.ALL);
It is also possible to use advanced methods
LogConfiguration config = new LogConfiguration.Builder().logLevel(BuildConfig.DEBUG ? LogLevel.ALL // Specify the log level, logs below this level will not be printed, default is LogLevel.ALL: LogLevel.NONE).tag(“MY_TAG”) // Specify TAG, default is “X-LOG”.enableThreadInfo() // Allow thread information to be printed, disable .enableStackTrace(2) by default, // Allow print depth to be 2 .enableBorder() is allowed to be printed by default, .jsonFormatter(new MyJsonFormatter()) is disabled by default, and the default is DefaultJsonFormatter.xmlFormatter(new MyXmlFormatter()) // Specify the XML formatter by default DefaultXmlFormatter.throwableFormatter(new MyThrowableFormatter()) // Specify a throwable exception formatter, default is DefaultThrowableFormatter.threadFormatter(new MyThreadFormatter()) // Specify the thread information formatter, default DefaultThreadFormatter.stackTraceFormatter(new MyStackTraceFormatter()) // Specifies the call stack information formatter and defaults to DefaultStackTraceFormatter.borderFormatter(new MyBoardFormatter()) // Specifies the border formatter and defaults to DefaultBorderFormatter.addObjectFormatter(AnyClass.class, // Add an object formatter for the specified typenew AnyClassObjectFormatter()) // Object.toString().addInterceptor(new) is used by default BlacklistTagsFilterInterceptor( // Add a blacklist TAG filter “blacklist1”, “blacklist2”, “blacklist3”)).addInterceptor(new MyInterceptor()) // Add a log interceptor.build(); Printer androidPrinter = new AndroidPrinter(true); Printer that prints logs via android.util.Log consolePrinter = new ConsolePrinter(); Print the log to the printer in the console via System.out Printer filePrinter = new FilePrinter // Print the log to the printer of the file. Builder(“<Log directory full path>”) // Specify the path where the log file is saved.fileNameGenerator(new DateFileNameGenerator()) // Specify the log file name generator, defaults to ChangelessFileNameGenerator(“log”).backupStrategy(new NeverBackupStrategy()) // Specify the log file backup policy, default is FileSizeBackupStrategy(1024 * 1024).cleanStrategy(new FileLastModifiedCleanStrategy(MAX_TIME)) // Specify the log file cleanup policy, default is NeverCleanStrategy(). flattener(new MyFlattener()) // Specify the log tiler, default is DefaultFlattener.writer(new MyWriter()) // Specify the log writer, default is SimpleWriter.build(); XLog.init( // Initialize XLogconfig, // Specify the log configuration, if not, use new LogConfiguration.Builder().build()androidPrinter by default, // Add as many printers as you want. If no printer is added, AndroidPrinter(Android)/ConsolePrinter(java)consolePrinter,filePrinter will be used by default);
3. Print the log
Print a simple message
XLog.d(“Hello World”);
Prints a throwable message, usually when an exception is thrown
XLog.e(message, throwable);
String formatting is supported
XLog.d(“Hello%s, I’m %d years old”, “Elvis”, 20);
Unformatted JSON and XML strings are automatically formatted
XLog.json(JSON_CONTENT); XLog.xml(XML_CONTENT);
All Collection and Map types of data are supported
XLog.d(array); XLog.d(list); XLog.d(map);
Intents and bundle objects can be printed directly
XLog.d(intent); XLog.d(bundle);
Save the log to a file
To save logs to a file, you need to create a FilePrinter
Printer filePrinter = new FilePrinter // Print the log to the printer of the file. Builder(“< full path to the log directory>”) // Specify the path where the log file is saved.fileNameGenerator(new DateFileNameGenerator()) // Specify the log file name generator, defaults to ChangelessFileNameGenerator(“log”).backupStrategy(new NeverBackupStrategy()) // Specify the log file backup policy, default is FileSizeBackupStrategy(1024 * 1024).cleanStrategy(new FileLastModifiedCleanStrategy(MAX_TIME)) // Specify the log file cleanup policy, default is NeverCleanStrategy(). flattener(new MyFlattener()) // Specifies the log tiler, defaults to DefaultFlattener.build();
And add it on initialization
XLog.init(config, filePrinter);
Architecture:
In addition, xLog also supports functions such as saving logs printed by third-party libraries to files, customizing log file names, intercepting and overthinking logs, customizing log formats, and formatting any type of object.