XLog

A simple log printing framework (supports customization of printing policies. By default, two policies are provided: LogCAT printing and disk printing)

About me

The characteristics of

  • Supports custom log format policiesIFormatStrategyAnd print strategyILogStrategy.
  • Two default log printing modes are provided: PrettyFormatStrategy (LogCAT) and Disk printing (PrettyFormatStrategy).
  • It is compatible with Android logcat, VERBOSE, DEBUG, INFO, WARN, ERROR, and WTF
  • Breaking logcat’s 4000 word limit
  • Supports printing XML, JSON, template String and other forms.
  • Supports custom log file storage (such as file prefix and time slice).
  • At the top of the log file, XLog provides a lot of useful runtime environment-related information, such as operating system information, device information, and application information
  • Time zone Settings are supported.
  • Log file information can be optionally printed.
  • Supports printing thread information.
  • The number of print methods supported.
  • Supports capturing and printing crash logs.
  • Support for custom crash log handling.
  • Third-party print interface adaptation is supported.

1. Demo (please support star)

1.1 logCAT printing effect

  1. Displays debugging information and JSON logs

  1. Printing XML information

  1. Print error message

1.2 disk printing effect

  1. Displays debugging information and JSON logs

  1. Printing XML information

  1. Print error message

2. How to use it

AndroidStudio supports the use of mainstream development tools, directly configure build.gradle, add dependencies can be.

2.1, Android Studio import method, add Gradle dependency

1. Add build.gradle repositories in the project root directory:

allprojects {
     repositories {
        ...
        maven { url "https://jitpack.io" }
    }
}
Copy the code

2. Add to dependencies:

dependencies { ... Implementation 'com. Making. Xuexiangjys: XLog: 1.1.3'}Copy the code

2.2. Initialization

Class in Application

XLog.init(this);
Copy the code

2.3. Build Logger

1. Building a Logger requires a log formatting strategy IFormatStrategy and a log printing strategy ILogStrategy.

The log formatting policy IFormatStrategy should hold the log printing policy ILogStrategy. Logger holds the log formatting policy.

  • XLog is responsible for scheduling global log loggers.

  • The Logger is responsible for providing the API of log printing externally.

  • IFormatStrategy is responsible for formatting and displaying log content.

  • ILogStrategy is responsible for the specific log printing.

The log printing process is as follows:

XLog -> Logger -> IFormatStrategy -> ILogStrategy
Copy the code

Here is a custom way to build a disk print Logger:

ILogStrategy diskLogStrategy = diskLogStrategy. NewBuilder () / / log print strategy. SetLogDir (" xlogDemo ") / / set the root directory of the log file storage .setLogprefix ("xlog") // Set the prefix of the log file name. SetLogSegment (logsegment.four_hours) // Set the interval between log segments. SetLogLevels (loglevel.error, Loglevel.debug) // Set the level of logging. Build (); IFormatStrategy formatStrategy = DiskFormatStrategy. NewBuilder () / / log format strategy setShowThreadInfo (false) / / set the thread information .settimeformat (timeutils.log_line_time) // Sets the time format for logging time. SetMethodCount (1) // Sets the number of methods to print. SetLogStrategy (diskLogStrategy) // Set the log printing policy. Build (); Logger.newbuilder ("DiskLogger").setformatstrategy (formatStrategy) // set log formatStrategy.build();Copy the code

2. Simple log Logger construction method.

To facilitate Logger construction, I provided LoggerFactory, a static Logger production factory. It contains several common Logger constructors.

  • GetLogger: Gets a custom assembled logger

  • GetPrettyLogger: Get nice logger [print logcat]

  • GetPrettyFormatStrategy: Get a nice log print format

  • GetDiskLogStrategy: Obtains the disk printing policy

  • GetDiskFormatStrategy: Gets the format strategy of disk printing

  • GetSimpleDiskFormatStrategy: strategy simplified disk print format

  • GetSimpleDiskLogger: Gets a simplified disk print logger

Here is a disk print Logger built using LoggerFactory:

DiskLogStrategy diskLogStrategy = LoggerFactory.getDiskLogStrategy(
        "xlogDemo", "xlog", LogLevel.ERROR, LogLevel.DEBUG
);
LoggerFactory.getSimpleDiskLogger("DiskLogger", diskLogStrategy, 0);
Copy the code

2.4. Log recording

UserInfo userInfo = new UserInfo().setLoginName("xuexiang").setPassword("12345678"); String json = new Gson().toJson(userInfo); XLog.get().d(json); Xlog.get (). Json (json); / / print information XLog json. The get (). The XML (ResourceUtils. ReadStringFromAssert (this, "androidmanifest.xml")); Try {throw new NullPointerException(" Error! ") ); } catch (Exception e) { XLog.get().e(e); // Prints an error message}Copy the code

2.5. Third-party Log Interface adaptation

When we use third-party libraries, it is inevitable to print logs showing third-party libraries to Logcat or disk. What should we do then?

At this point you can use the Logger log method for interface adaptation.

Logger. SetLogger (new ILogger() {@override public void log(int priority, String tag, String message, Throwable t) { XLog.get().getLogger("LogUtils").log(LoggerFactory.logPriority2LogLevel(priority), tag, message, t); }});Copy the code

The log method has the following interface:

/** * Log print ** @param level Log print level * @param tag Log tag * @param message Log information * @param Throwable error information */ void  log(@LogLevel String level, String tag, String message, Throwable throwable);Copy the code

2.6 Program Crash processing

There are currently two default Crash handling options available:

  • ToastCrashListener: Simple toast prompt + program starts automatically.

  • SendEmailCrashListener: sends crash log emails.

CrashHandler.getInstance().setOnCrashListener(new ToastCrashListener());
CrashHandler.getInstance().setOnCrashListener(new SendEmailCrashListener());
Copy the code

Of course, you can implement your own Crash handling by implementing the OnCrashListener interface.

Special thanks to

Github.com/orhanobut/l…

Github.com/JiongBull/j…

contact

More information, welcome wechat search public number: “My Android open source journey”