java - Logback usage and printing lists -


I am starting to use logback and I want to know whether there are better ways to do something. I have this code:

  Public class class A {Private listing & lt; String & gt; L; Private logger logger; Public category () {this.logger = LoggerFactory.getLogger (this.getClass (). GetName ()); } ... public list & lt; String & gt; Method () {this.logger.debug ("method ()"); & Lt; String & gt; Name; Try {Names = otherClass.getNames (); } Hold (exception e) {string msg = "error in getting name"; This.logger.error (MSG); Throw New Class Exception (msg, e); } This.logger.debug ("name: {}", XXXxx); Name back; }  

I still have some doubts:

  • In each class there will be a this.logger = LoggerFactory.getLogger (this.getClass () )
  • Each method will have a this.logger.debug ("method ()"); To know when a method is called.

Does not it seem good that there is any way to solve it?

Also I want to print a list in this list in .log: this.logger.debug ("names: {}", xxxxx);

The xxxxx should be replaced with something to print the list. An anonymous class?

Thanks for reading!

Use it and you can use it. Compile your code with AJC compiler instead of Javac and then run normally with Java executable.

You need the aspectjrt.jar and log4j.jar on classpath.

  Import org.aspectj.lang. *; Import org.apache.log4j *; Public aspect tracemitskales (Logger Logger = Logger Tag ("trace"); TracemithColls () {Logar .Set Level (Level.AL); } Pointcut traceMethods () // Let me give each visual call of each view with each visibility: (execution (* * *. * (..)) // I also have to give Constructor Call || execution (* .new ..))) // stop recursion does not get the same method call in this aspect category & amp; ! (TraceMethodCalls); // Advice First: Doing something before the method is actually executed before (): traceMethods (if (logger.isEnabledFor (Level.INFO)) {// Get information about the captured method and log it Signature sig = thisJoinPointStaticPart.getSignature (); Logger.log (level .INFO, "logging [" + sig.getDeclaringType (). GetName () + "." + Sig.getName () + "]");} }}  

Look for documents known about how to change the TraceMethodCalls calls.

  // eg Copy only the public method calls: I Changes: (Execution (* *. * (..)) // In: (Execution (public * *. * (..))  

In relation to

Also, in this list, a list is printed in this list: this.logger.debug ("names: {}", xxxxx);

< / Blockquote>

This is supported by default by slf4j / logback. Just do

  logger.debug ("name: {}", name);  

For example

  list & lt; string & gt; List = new array list & lt; String & gt; (); List.add ("test1"); List.add ("Test2"); List.add ("test3"); Logger.debug ("name: {}", list); // output //xx::xx.xxx [main] debug [classname] - names: [test1, test2, test3]  

Want different?


Comments

Popular posts from this blog

c++ - Linux and clipboard -

What is expire header and how to achive them in ASP.NET and PHP? -

sql server - How can I determine which of my SQL 2005 statistics are unused? -