我希望我的网络应用程序使用此路径登录文件:webapp/logs /
我可以在log4j.properties文件中设置绝对路径,但生产环境的目录结构会有所不同.有什么办法可以吗?
我是这样做的:
log4j.appender.f=org.apache.log4j.RollingFileAppender
log4j.appender.f.layout=org.apache.log4j.PatternLayout
log4j.appender.f.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
log4j.appender.f.File=log.out
log4j.appender.f.MaxFileSize=100KB
Run Code Online (Sandbox Code Playgroud)
这是将日志打印到我的eclipse目录(c:// eclipse)中名为log.log的文件中.我正在使用Tomcat 6.
我正在开发一个网络应用程序,我想记录一些信息,以帮助我改进和观察应用程序.(我正在使用Tomcat6)
首先我想我会使用StringBuilders,将日志附加到它们,并且任务会像每2分钟一样将它们保存到数据库中.因为我担心开箱即用的日志记录系统的性能.然后我做了一些测试.特别是log4j.
这是我的代码:
Main.java
public static void main(String[] args) {
Thread[] threads = new Thread[LoggerThread.threadsNumber];
for(int i = 0; i < LoggerThread.threadsNumber; ++i){
threads[i] = new Thread(new LoggerThread("name - " + i));
}
LoggerThread.startTimestamp = System.currentTimeMillis();
for(int i = 0; i < LoggerThread.threadsNumber; ++i){
threads[i].start();
}
Run Code Online (Sandbox Code Playgroud)
LoggerThread.java
public class LoggerThread implements Runnable{
public static int threadsNumber = 10;
public static long startTimestamp;
private static int counter = 0;
private String name;
public LoggerThread(String name) {
this.name = name;
}
private …Run Code Online (Sandbox Code Playgroud) 我正在编写一个连接到网站并从中读取一行的应用程序.我是这样做的:
try{
URLConnection connection = new URL("www.example.com").openConnection();
BufferedReader rd = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String response = rd.readLine();
rd.close();
}catch (Exception e) {
//exception handling
}
Run Code Online (Sandbox Code Playgroud)
好吗?我的意思是,我在最后一行关闭了BufferedReader,但我没有关闭InputStreamReader.我应该从connection.getInputStream创建一个独立的InputStreamReader,还是从独立的InputStreamReader创建一个BufferedReader,而不是关闭所有两个读者?我认为最好将结束方法放在finally块中,如下所示:
InputStreamReader isr = null;
BufferedReader br = null;
try{
URLConnection connection = new URL("www.example.com").openConnection();
isr = new InputStreamReader(connection.getInputStream());
br = new BufferedReader(isr);
String response = br.readLine();
}catch (Exception e) {
//exception handling
}finally{
br.close();
isr.close();
}
Run Code Online (Sandbox Code Playgroud)
但它很难看,因为关闭方法可以抛出异常,所以我必须处理或抛出它.
哪种解决方案更好?或者什么是最好的解决方案?
有什么办法可以在JavaScript中捕获任何未捕获的异常?我的意思是,我所有的“危险”代码都在try-catch块中。但是我没有明确处理的异常呢?我正在使用jQuery,我的主要javascript文件开头为:
$(document).ready(function(){})
在这里,我将一些事件绑定到某些DOM元素。我可以在这里使用try-catch块,但是它们将捕获在事件绑定过程中而不是在事件处理过程中发生的异常。但是,如果我在每个事件处理函数中都使用try-catch块,那将很丑陋。
我应该如何捕获显式try-catch块中未发生的异常?(我不想编写通用处理程序函数,我只想将问题发送到我的服务器)
我一直在Eclipse中开展动态Web项目.我最近重新安装了我的操作系统,并使用了一个干净的日食.我可以导入我的web项目,但是当我尝试午餐(在服务器上运行)时,我总是收到此消息:
"The selection cannot be run on any server"
Run Code Online (Sandbox Code Playgroud)
我安装了eclipse的tomcat插件,它运行正常.我也设置了tomcat库.(窗口/偏好/ Tomcat)的.
有什么问题呢?