You can always use log4j and configure appender to both console and file.
only
【在 l**********n 的大作中提到】 : when I run junit test, how can I log the java log to a file? the log is only : logged to a file when running inside glassfish
a*p
11 楼
独显用的电源适配器要大一点
l*n
12 楼
where should I put the log4j configuration file? in the resources folder of the java source or test resources folder?
【在 g*****g 的大作中提到】 : You can always use log4j and configure appender to both console and file. : : only
g*g
13 楼
It needs to be in classpath, that's all.
of
【在 l**********n 的大作中提到】 : where should I put the log4j configuration file? in the resources folder of : the java source or test resources folder?
l*n
14 楼
java.util.logging.Logger' is already defined in a single-type import I found I couldn't switch to log4j.
l*n
15 楼
in the code, I set up like this: private static void setupLogger(){ m_log.setLevel(Level.ALL); try { FileHandler fhandler = new FileHandler("E:\logs\service.log"); SimpleFormatter sformatter = new SimpleFormatter(); fhandler.setFormatter(sformatter); m_log.addHandler(fhandler); } catch (IOException ex) { m_log.log(Level.SEVERE, ex.getMessage(), ex); } catch (SecurityException ex) { m_log.log(Level.SEVERE, ex.getMessage(), ex); } } static { setupLogger(); } // but then how I know it is production or unit testing? how to enable logging globally?
l*n
16 楼
I added the logging.properties to classpath, it is not working. handlers=java.util.logging.FileHandler, java.util.logging.ConsoleHandler java.util.logging.ConsoleHandler.level=INFO java.util.logging.ConsoleHandler.formatter=java.util.logging.SimpleFormatter java.util.logging.FileHandler.level=INFO java.util.logging.FileHandler.pattern=logs/jetty.log # Write 10MB before rotating this file java.util.logging.FileHandler.limit=10000000 # Number of rotating files to be used java.util.logging.FileHandler.count=4 java.util.logging.FileHandler.formatter=java.util.logging.SimpleFormatter .level=INFO
我们是个小公司,没有什么framework,已经用了java.util.logging.Logger, 还可以 用log4j吗? 怎么set up aspectj? 怎么分离log和真正的代码?log on method entry point? what about to log some business logic inside the method?
l*n
19 楼
I don't understand Logger.getLogger(getClass().getName()) it seems creating so many logger is unnecessary. why not keep only one Logger? coz then it only has one place to add the Handler: m_log.addHandler(fhandler); otherwise it has to go to so many different logger and for each one, add the FileHandler. I am confused of Java.
l*n
20 楼
there should be a global logger: static public void setup() throws IOException { // Get the global logger to configure it Logger logger = Logger.getLogger(Logger.GLOBAL_LOGGER_NAME); logger.setLevel(Level.INFO); fileTxt = new FileHandler("Logging.txt"); fileHTML = new FileHandler("Logging.html"); // Create txt Formatter formatterTxt = new SimpleFormatter(); fileTxt.setFormatter(formatterTxt); logger.addHandler(fileTxt); // Create HTML Formatter formatterHTML = new MyHtmlFormatter(); fileHTML.setFormatter(formatterHTML); logger.addHandler(fileHTML); }