l**********n 发帖数: 8443 | 1 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 |
c*****a 发帖数: 808 | |
g*****g 发帖数: 34805 | 3 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
|
l**********n 发帖数: 8443 | 4 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 发帖数: 34805 | 5 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 发帖数: 8443 | 6 java.util.logging.Logger' is already defined in a single-type import
I found I couldn't switch to log4j. |
l**********n 发帖数: 8443 | 7 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 发帖数: 8443 | 8 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 |
z****e 发帖数: 54598 | 9 前面说了log4j
我建议上aspectj
如果你会用的话,就可以很容易滴将log代码和真正逻辑代码分离 |
l**********n 发帖数: 8443 | 10 我们是个小公司,没有什么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 发帖数: 8443 | 11 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 发帖数: 8443 | 12 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);
} |