ALog Documentation

repository·master·Indexed 19 days ago

https://github.com/blankj/alog

An Android logging utility providing enhanced Logcat visibility, support for long strings, multi-parameter logging, and automatic formatting for types like Array, Throwable, Bundle, and Intent. It includes features for writing logs to files with automatic rotation and retention. Note: This library is no longer maintained; users are directed to LogUtils from AndroidUtilCode.

Tokens
1.6K
Snippets
4
Records
5
Agent score
16%

What's inside ALog

  1. Initialize ALog in your Application

    master

    Initialize ALog within the onCreate method of your Application class using the ALog.init(Context) method. This returns an ALog.Config object that allows you to configure various logging behaviors such as console output, file storage, and global tags.

    // init it in ur application
    public void initALog() {
        ALog.Config config = ALog.init(this)
                .setLogSwitch(BuildConfig.DEBUG)// Set total log switch (console + file)
                .setConsoleSwitch(BuildConfig.DEBUG)// Set console output switch
                .setGlobalTag(null)// Set global tag (null uses class name if no tag provided)
                .setLogHeadSwitch(true)// Set log header switch
                .setLog2FileSwitch(false)// Set log to file switch
                .setDir("")// Set directory (defaults to /cache/log/ if empty)
                .setFilePrefix("")// Set file prefix (defaults to "alog")
                .setBorderSwitch(true)// Set border switch
                .setSingleTagSwitch(true)// Set single tag switch for AS 3.1 Logcat
                .setConsoleFilter(ALog.V)// Console filter (e.g., ALog.V)
                .setFileFilter(ALog.V)// File filter
                .setStackDeep(1)// Stack depth
                .setStackOffset(0)// Stack offset
                .setSaveDays(3)// Days to keep logs (-1 for infinite)
                .addFormatter(new ALog.IFormatter<ArrayList>() {
                    @Override
                    public String format(ArrayList list) {
                        return "ALog Formatter ArrayList { " + list.toString() + " }";
                    }
                });
        ALog.d(config.toString());
    }
  2. Configure ALog settings

    master

    Use the ALog.Config object returned by ALog.init() to customize logging behavior. Available configuration methods include:

    • setLogSwitch(boolean): Master switch for all logging.
    • setConsoleSwitch(boolean): Enable/disable Logcat output.
    • setGlobalTag(String): Set a global tag for all logs.
    • setLogHeadSwitch(boolean): Enable/disable log header info (thread name, class, line number).
    • setLog2FileSwitch(boolean): Enable/disable writing logs to files.
    • setDir(String): Set the directory for log files.
    • setFilePrefix(String): Set the prefix for log files.
    • setBorderSwitch(boolean): Enable/disable log borders.
    • setSingleTagSwitch(boolean): Output one log per line (improves Android Studio 3.1 Logcat view).
    • setConsoleFilter(int): Set the Logcat level filter.
    • setFileFilter(int): Set the file logging level filter.
    • setStackDeep(int): Set the stack trace depth.
    • setStackOffset(int): Set the stack trace offset.
    • setSaveDays(int): Set how many days to retain log files.
    • addFormatter(IFormatter<T>): Add a custom formatter for specific types.
  3. Use ALog for logging

    master

    ALog provides several methods for logging with different severity levels and specialized formats. If no tag is provided, it defaults to the current class name.

    // Standard logging with class name as tag
    ALog.v("message");
    ALog.d("message");
    ALog.i("message");
    ALog.w("message");
    ALog.e("message");
    ALog.a("message");
    
    // Logging with a custom tag
    ALog.vTag("MyTag", "message");
    ALog.dTag("MyTag", "message");
    ALog.iTag("MyTag", "message");
    ALog.wTag("MyTag", "message");
    ALog.eTag("MyTag", "message");
    ALog.aTag("MyTag", "message");
    
    // Specialized logging
    ALog.d("MyTag", "arg1", "arg2"); // Multi-parameter output
    ALog.json("{\"key\":\"value\}"); // JSON string output
    ALog.xml("<xml></xml>"); // XML string output
    ALog.file("long string"); // Write specifically to file
  4. Add a custom Log Formatter

    master

    You can extend ALog's capabilities by implementing the ALog.IFormatter<T> interface. This allows you to define how specific object types are converted into log strings. Use addFormatter on the Config object during initialization.

    .addFormatter(new ALog.IFormatter<ArrayList>() {
        @Override
        public String format(ArrayList list) {
            return "Custom Format: " + list.toString();
        }
    });