Android uses Log4j to write logs to the SD card and dynamically modify the name of the output file

Source: Internet
Author: User

Android uses Log4j to write logs to the SD card and dynamically modify the name of the output file


1. Log4j simple use 1. Download log4j. jar http://logging.apache.org/log4j/2.x/
2. Create Java code

public class Loggers {    public static Logger logger =            Logger. getLogger(Loggers. class);    public static void init() {        try {            PatternLayout patternLayout = new PatternLayout();            patternLayout.setConversionPattern("%d{yyyy-MM-dd HH:mm:ss} %m %n");            FileAppender fileAppender = new FileAppender(patternLayout, "D://log4j_info.log" );            logger.addAppender(fileAppender);                   } catch (IOException e) {            e.printStackTrace();        }      }          public static void main(String argv[]) {        init();               logger.debug( "Hello, my name is Homer Simpson." );        logger.info( "We are the Simpsons!" );        logger.warn( "Mmm...forbidden donut." );        logger.error( "Dear Baby, Welcome to Dumpsville. Population: you.");        logger.fatal( "Eep.");           }}


The code in init is to set the path and output file name based on different mobile phones, so use the code to configure the output path.
2. Android uses Log4j to write logs to the SD card and dynamically modify the name of the output file
1. Use the following code to set the output path and call it in the above init method.
Private static String getOutputPath (Context context, String name) {String rootPath = getSdcardPath (context); StringBuilder fileName = new StringBuilder (); // mobile phone model fileName. append (android. OS. build. MODEL); fileName. append ("-"); // System Version fileName. append ("Android _"); fileName. append (android. OS. build. VERSION. RELEASE); // SDK version fileName. append ("_"); fileName. append (android. OS. build. VERSION. SDK); fileName. append ("-"); String path = rootPath + "/" + fileName. toString () + name; return path;} private static String getSdcardPath (Context context) {// whether the SD card contains if (android. OS. environment. getExternalStorageState (). equals (android. OS. environment. MEDIA_MOUNTED) {// the SD card exists. return the root directory of the SD card, return Environment. getExternalStorageDirectory (). getPath ();} else {return context. getFilesDir (). getPath ();}}



2. Set SD card read and write permissions
      
       
       
       
     



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.