Giter VIP home page Giter VIP logo

log-to-file's Introduction

Android LogUtils 日志文件本地化

引入方法

  1. 在Application中初始化

    public class MegApplication extends Application {
        @Override
        public void onCreate() {
            super.onCreate();
            initLog();
        }
    
        private void initLog() {
            String dir = Environment.getExternalStorageDirectory().getAbsolutePath();
            File file = new File(dir + File.separator + "Meg-Logs");
            if (!file.exists()) file.mkdirs();
            LogUtils.setLogDir(file.getAbsolutePath());
            LogUtils.setLogLevel(LogUtils.LogLevel.DEBUG);
        }
    }
  2. 使用

    类似Android中的Log类的使用方法。

    public class MainActivity extends AppCompatActivity {
        @Override
        protected void onCreate(@Nullable Bundle savedInstanceState) {
           LogUtils.d(TAG, "--start--");
           LogUtils.e(TAG, e.toString());
           LogUtils.d(TAG, "--after dangerous method call--");
           LogUtils.w(TAG, "--test--");
    
        }
    
        @Override
        protected void onDestroy() {
            super.onDestroy();
            LogUtils.close();
        }
    }

    注意退出时需调用LogUtils.close(); 将队列中缓存的log,flush到本地文件中,不然会有log缺失的问题

    通过以下项配置最大日志文件数,和单一日志文件大小

    private static final int LOG_FILES_MAX_NUM = 5; //文件最多有5个
    private static final int LOG_FILE_MAX_SIZE = 1024 * 1024; //文件最大1MB

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.