Hi Ralph,
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 the 
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...
Thank you very much Ralph
Kindest regards 

     El Sábado 31 de enero de 2015 21:19, Ralph Goers 
<[email protected]> escribió:
   

 You can already do:
ConfigurationSource source = new 
ConfigurationSource(config.getBytes(“UTF-8”));Configurator.initialize(this.getClass().getClassLoader(),
 source);
Or are you suggesting that there should be some string variable that Log4j 
knows about that is somehow magically set?
Maybe I’m not understanding what you want to do.
Also, you say that “everything explodes”.  Do you have a stack trace for us to 
look at?
Ralph




On Jan 31, 2015, at 9:05 AM, Alvaro de Lucas <[email protected]> wrote:
Hi there, 


First of all, sorry about my poor english. I hope you'll understand this 
email... and this is the right way to do this...


I would try to explain in 2 minutes to what I think it could be a good 
improvement for the amazing log4j2 library...(in case it doesn't already 
exist...)
I'm developing a service (based on a J2EE web server and an App) and I've been 
working with log4j v2 intensively last days using it in a J2EE project.
Now I'm trying to upgrade the log4j v1 I used in the app to v2, and I would 
like to use a similar log4j2.xml file I used on the web server.
I've been unable to use log4j2 in the App in the same way I use it in the web 
server (probably because its a servlet 3.0 and it uses  
Log4jServletContextListener and Log4jServletFilter...).There, you just need to 
use LogManager.getLogger ("foo") to load the foo logger described in the 
log4j2.xml, and everything is quite easy...
But as I said, I think I'm not able to load the log4j2.xml on Android 
environment (and I put the file in every directory :-)... and when I call 
LogManager.getLogger ("foo") everything explodes (it raises and exception...)
I've read the manual, I looked into the API, I searched into all kind of 
forums, ... but I've just found a lot of people in the same situation and I 
think I'm looking for something that doesn't exist...
In the situation I describe, I think the typical solution is to configure 
everything from scratch programatically... but it will be complex and difficult 
to do and manage...
I think the best solution in this case would be to provide the log4j2.xml 
information directly into a String... (instead putting the file in a concrete 
place and wait for the lib to find it, it would be great to be able to give it 
directly the content of the file into a String and use this alternative when 
there's no way to load the file...
I've found a lot of people trying to put the file here and there looking for 
the lib to find it when it looks easier to send the configuration information 
direcly into a String variable...

Does it have any sense for you?Does this already exist? is there a way to do it 
using what already exists?

Thank you very much in advanceKindest regards
(Yesterdary I posted about this on StackOverflow here: 
http://stackoverflow.com/questions/28235021/log4j2-on-android)
    



   

Reply via email to