Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to solve the problem of relative path in Log4j

2025-01-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces the relevant knowledge of "how to solve the problem of relative path in Log4j". Many people will encounter such a dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

1. In properties file:

Log4j.appender.logfile.File=$ {WORKDIR} / logs/app.log

Where "${WORKDIR} /" is a variable and will be replaced by the value of "WORKDIR" in System Property. In this way, we can set the root path with System.setProperty before log4j loads the configuration file.

In jsp, you can do this:

String path = pageContext.getServletContext () .getRealPath ("/")

String realPath = path+ "WEB-INFclasseslog4j.properties"

System.setProperty ("WORKDIR", path.replace ("," / "))

PropertyConfigurator.configure (realPath.replace ("", "/")); / / load the .properties file

[@ more@]

Second, you can use environmental variables (I have never tried to read it online)

Log4j.appender.R=org.apache.log4j.RollingFileAppender

Log4j.appender.R.File=$ {catalina.base} / logs/logs_tomcat.log

Log4j.appender.R.MaxFileSize=10KB

Third, the concrete realization: (I have not tried to read it on the Internet)

Generally speaking, in the course of our development project, the output path of the log4j log is fixed to a certain folder, so if I change the environment, the log path needs to be modified again, which is not convenient. At present, I use the method of dynamically changing the log path to save the log file relative to the path.

(1)。 When the project starts, load the initialization class:

Public class Log4jInit extends HttpServlet {

Static Logger logger = Logger.getLogger (Log4jInit.class)

Public Log4jInit () {}

Public void init (ServletConfig config) throws ServletException {

String prefix = config.getServletContext () .getRealPath ("/")

String file = config.getInitParameter ("log4j")

String filePath = prefix + file

Properties props = new Properties ()

Try {

FileInputStream istream = new FileInputStream (filePath)

Props.load (istream)

Istream.close ()

/ / toPrint (props.getProperty ("log4j.appender.file.File"))

String logFile = prefix + props.getProperty ("log4j.appender.file.File"); / / set the path

Props.setProperty ("log4j.appender.file.File", logFile); PropertyConfigurator.configure (props)

/ / load log4j configuration information

} catch (IOException e) {

ToPrint ("Could not read configuration file [" + filePath + "].")

ToPrint ("Ignoring configuration file [" + filePath + "].")

Return

}

}

Public static void toPrint (String content) {

System.out.println (content)

}

}

(2) configuration in .Web.xml

Log4j-init

Log4jInit

Log4j

WEB-INF/classes/log4j.properties

This is the end of the content of "how to solve the problem of relative paths in Log4j". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report