Log4j2 on Android

前端 未结 2 529
Happy的楠姐
Happy的楠姐 2021-01-05 23:41

I\'m trying to use log4j2 on Android Project (I\'m working on Android Studio).

In order to simplify this question, I will explaine what I\'ve done in a simple dummy

2条回答
  •  自闭症患者
    2021-01-05 23:58

    I've finally have what I was looking for...

    I'm working with Android, and I was unable to recover loggers by name (LogManager.getLogger ("xxx") crashes the App in the worst way possible...)

    I think the problem starts when it looks for the log4j2.xml. I put the file everywhere, but it doesn't work... ...so, I wanted to provide the log4j2.xml content into a String...

    Here it is what I've done...

        String log4j2xmlFileContent=getLog4j2xmlContent();//Content of my log4j2.xml
    
        // convert String into InputStream
        InputStream is = new ByteArrayInputStream(log4j2xmlFileContent.getBytes());
    
        ConfigurationSource source=null;
        try{
            source = new ConfigurationSource(is);
        } catch (IOException ioe){
            ioe.printStackTrace();
        }
    
        Configuration config = org.apache.logging.log4j.core.config.xml.XmlConfigurationFactory.getInstance().getConfiguration(source);
        loggerContext = (LoggerContext) LogManager.getContext();
        try {
            //loggerContext.stop();
            loggerContext.start(config);
        } catch (Exception e){
            e.printStackTrace();
        }
        return loggerContext;
    

    and now, I can use that loggerContext to get my loggers...

        loggerHitosCarga = context.getLogger("HitosCarga");
        loggerPeticiones = context.getLogger("Peticiones");
        loggerQueries = context.getLogger("Queries");
        loggerDepuracionActual=context.getLogger("DepuracionActual");
    
        loggerDepuracionActual.warn("FUNCIONAAAAA!!!!..");        
        loggerHitosCarga.info("Loggers inicializados...");
    

    Now I only have to review and improve it a little bit, but it works...

提交回复
热议问题