OC Hierarchical log tool, which supports four log levels and two persistence schemes

Source code address: github.com/FinderTiwk/…

use

#import "Log4OC.h"

    //DEBUGLog (recommended for development and debugging)
    DEBUGLog(@" This is a DEBUG level log");
    DEBUGLog(@"FinderTiwk".@" This is a DEBUG level log with an author");
    
    //INFOLog(it is recommended to record key information)
    INFOLog(@" This is an INFO level log");
    INFOLog(@"FinderTiwk".@" This is an INFo-level log with an author");
    
    //WARNINGLog(it is recommended to record warning sensitive information)
    WARNINGLog(@" This is a WARNING log");
    WARNINGLog(@"FinderTiwk".@" This is a WARNING level log with an author");
    
    //ERRORLog(recommended for recording abnormal errors)
    ERRORLog(@" This is an ERROR level log");
    ERRORLog(@"FinderTiwk".@" This is an ERROR level log with an author"); -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- the Console Print -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- [🐞 DEBUG]2018- 11- 14 12:53:45: This is a DEBUG level log [🐞DEBUG]2018- 11- 14 12:53:45(FinderTiwk): This is a DEBUG level log with an author [🌴INFO]2018- 11- 14 12:53:45: This is an INFO level log [🌴INFO]2018- 11- 14 12:53:45(FinderTiwk): This is an info-level log with an author [⚠️WARNING]2018- 11- 14 12:53:45: This is a WARNING log [⚠️WARNING]2018- 11- 14 12:53:45(FinderTiwk): This is a WARNING level log with an author [🔴ERROR]2018- 11- 14 12:53:45: This is an ERROR level log [🔴ERROR]2018- 11- 14 12:53:45(FinderTiwk): This is an ERROR level log with an authorCopy the code

preferences

0x00 Log level

LogLevel DESC
LogLevelDEBUG The default; Shows including (DEBUGLog, INFOLog WARNINGLog, ERRORLog Log)
LogLevelINFO Shows including (INFOLog WARNINGLog, ERRORLog Log)
LogLevelWARNING Displays logs including (WARNINGLog,ERRORLog)*
LogLevelERROR Only the logs of ERRORLog are displayed
LogLevelNONE Disable all Logs

#ifdef __OPTIMIZE__
    // It is recommended to remove DEBUG logs when packaging
    setLogLevel(LogLevelINFO);
#endif

// Get the current log level
LogLevel level = currentLogLevel();

Copy the code

0x01 Log Mode

  • mode:Logging mode
    • 0: prints only to the console,DEFAULT
    • 1: Saves logs to Sqlite
    • 2: Saves logs to a local file
  • clean:Specifies whether to automatically clear logs when the log mode is not 0
    • 0: does not automatically clear
    • Greater than 0: logs generated n days before the current date are automatically cleared
  • both:Whether log persistence is printed to the console at the same time when log mode is not 0
    • YES: Output to both console and file (DEBUG)
    • “NO” : output only to files
extern void setLogMode(NSUInteger mode,NSUInteger clean, BOOL both);
Copy the code

0x02 Log File Is Cut

If the log mode is set to 2 and local files are stored, set the log file size threshold. The unit is K and the default value is 1024K/1M. If a single.

Logs are saved in the sandbox, for example, 2018_XX_XX-1. log, 2018_XX_XX-2.log

extern void setLogMaxSize(NSUInteger threshold);
Copy the code

Log filter

  1. Filter by log level
  2. Filter by log writer name
  3. Filter by time

0x00 Console mode

Log level: You can observe logs with naked eyes using the printed log prefix or search keywords (DEBUG,INFO,WARNING,ERROR) using COMMAND+F in the console.

Author name: In the console COMMAND+F search for author name

Time: Console log output is in chronological order. You can view the timestamp in the log prefix yourself

0x01 In database mode

Database table structure, table name Logger

field The data type The default value describe
level INTEGER The level of logging
time DATE CURRENT_TIMESTAMP Print the time
content TEXT Log contents
author TEXT The author, DEAFULT Apple

Log level:

SELECT * FROM `Logger` WHERE level = 2
Copy the code

Author name:

SELECT * FROM `Logger` WHERE author = 'FinderTiwk'
Copy the code

Time:

SELECT * FROM `Logger` WHERE time > 'the 2018-11-13 12:12:12'
Copy the code

Multi-conditional filtering:

SELECT * FROM `Logger` WHERE author = 'FinderTiwk' AND level = 2 AND time > 'the 2018-11-13 12:12:12'
Copy the code

0x02 Local File Mode

Use Console of the Mac system to open the 2018_xx_XX-1. log file and search by keyword

P1-jj.byteimg.com/tos-cn-i-t2…