Reputation: 143
I have a dynamic web-app. I'm working in a spring environment, with maven and mybatis. I'm deploying a war file on tomcat. I'm using log4j for logging the data. I want this data to be accessible real time to everyone. For example if the home page of the web-app is localhost:8080
then I want something like localhost:8080/logs to display the logs real time. I know I have to Spring request mapping for this. But I don't know how to make it work, when my file keeps on updating itself.
Any help is appreciated
Upvotes: 0
Views: 114
Reputation: 690
You can use HTML Layout feature of log4j as following:
# Define the root logger with appender file
log4j.rootLogger = DEBUG, FILE
# Define the file appender
log4j.appender.FILE=org.apache.log4j.FileAppender
log4j.appender.FILE.File=${catalina.base}/webapps/yourappname/log.html
# Define the layout for file appender
log4j.appender.FILE.layout=org.apache.log4j.HTMLLayout
log4j.appender.FILE.layout.Title=HTML logs
log4j.appender.FILE.layout.LocationInfo=true
This file should be accessible on http://localhost:8080/yourappname/log.html
. Tomcat will provide catalina.base/catalina.home as system property or you can provide full file path.
Upvotes: 1
Reputation: 127
If it's a spring mvc.
In your controller, you need to read the file and write it into the response.
It's actually fair simple.
Code snippet for you as reference: The following is SUDO code. Just give you the direction
@RequestMapping("/log") public class Controller {
public void readLog(HttpRequest req, HttpResponse resp) {
File file = new File('YOUR_FILE_LOCATION')
FileReader fr = new FileReader(file);
// you can read every line for the log file and write into resposne
while(str = nextLine()) {
resp.println(str)
}
fr.close()
}
}
Upvotes: 4