折腾:
【未解决】java的log4j2中没有生成File类型的log文件
期间,感觉是:需要把log4j的classpath加到maven中。
log4j2 add xml to classpath
maven classpath classpathentry
maven .classpath classpathentry
maven add classpathentry
java .classpath file
The .classpath file is maintained by Eclipse’s JDT feature (feature = set of plugins
此处的
src/xxx/.classpath
是Eclipse的文件,不是此处标准的,maven的classpath文件
maven 设置 classpath
maven set classpath
maven set log4j classpath
maven set log4j2.xml classpath
感觉算是把 log4j2.xml,加到maven中
一种方式是:
放到:
src/main/resources/
中:
src/main/resources/log4j2.xml
但是此处不想这么放,想要搞懂,如何指定classpath,把log4j2.xml加进去
“Add log4j2.xml file in resources folder
Spring will automatically configure log4j2 once it finds it’s jar files in classpath. Now you have to add log4j2.xml or (log4j2.properties) in src/main/resources folder. That’s all.”
说是必须放到这里才行
Apache Log4j 2 Tutorials – Mkyong.com
说是放到:
src/resources/log4j2.xml
或:
src/resources/log4j2.xml
maven add log4j2.xml to classpath
如果不用maven,则是自己加jar包:
比如:
.classpath
<classpathentry kind="lib" path="lib/log4j-api-2.12.1.jar" /> <classpathentry kind="lib" path="lib/log4j-core-2.12.1.jar" />
然后放配置到src下:
src/log4j2.xml
如果VSCode中用maven,则是:
src/main/java/log4j2.xml
另外学到了,右键
pom.xml,有:
Update project Configuration
“Well you have to add log4j2.xml file to your project’s resource folder in order for log4j to run correctly with your project.”
必须把log4j2.xml加到项目的resource目录中,才能让log4j正常运行
即,放到:src/main/resources
vscode maven log4j2.xml location
里面没有明说,必须是:src/main/resources
只是这段
“How do I specify the configuration file location?
By default, Log4j looks for a configuration file named log4j2.xml (not log4j.xml) in the classpath.
You can also specify the full path of the configuration file with this system property: -Dlog4j.configurationFile=path/to/log4j2.xml
That property can also be included in a classpath resource file named log4j2.component.properties.”
大意是,要放到classpath中。
maven pom.xml add log4j2 xml
试试加配置到pom.xml
<systemProperties> <property> <name>/log4j.configuration</name> <value>.../../Foo-log4j.xml</value> </property> </systemProperties>
结果报错:
{ "resource": "/Users/crifan/dev/dev_root/projects/xxx/src/xxx/pom.xml", "owner": "_generated_diagnostic_collection_name_#0", "code": "cvc-complex-type.2.4.a", "severity": 8, "message": "Invalid element name:\n - systemProperties\n\nOne of the following is expected:\n - parent\n - packaging\n - name\n - description\n - url\n - inceptionYear\n - organization\n - licenses\n - developers\n - contributors\n - mailingLists\n - prerequisites\n - modules\n - scm\n - issueManagement\n - ciManagement\n - distributionManagement\n - dependencyManagement\n - dependencies\n - repositories\n - pluginRepositories\n - build\n - reports\n - reporting\n - profiles\n\nError indicated by:\n {http://maven.apache.org/POM/4.0.0}\nwith code:", "source": "xml", "startLineNumber": 16, "startColumn": 6, "endLineNumber": 16, "endColumn": 22 }
也说了,必须是:
+--src | +--main | +--java | +--com | +--memorynotfound | +--logging | |--Log4j2Example.java | +--resources | |--log4j2.xml pom.xml
这个结构
“ put your log4j2.xml file in the default package of your project.”
没看到default package
“Configuration of Log4j 2 can be accomplished in 1 of 4 ways:
1. Through a configuration file written in XML, JSON, YAML, or properties format.
2. Programmatically, by creating a ConfigurationFactory and Configuration implementation.
3. Programmatically, by calling the APIs exposed in the Configuration interface to add components to the default configuration.
4. Programmatically, by calling methods on the internal Logger class.”
vscode add maven classpath
“In case VS Code throws an error for a classpath issue, try setting your classpath manually by either setting the CLASSPATH environment variable or editing the .classpath file with the path to the JAR file:
<classpathentry kind=”lib” path=”lib/log4j.jar”/>”
即:VSCode中此处,也识别和支持 .classpath 文件
好像就是去给.classpath中加上对应配置
此处是希望加上log4j2.xml的路径,而不是jar包文件
所以去研究
classpathentry
” <classpathentry kind=”lib” path=”/opt/ABBYY/FREngine12/Inc/Java/com.abbyy.FREngine.jar” />”
<?xml version="1.0" encoding="UTF-8"?> <classpath> <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/> <classpathentry kind="src" path="src"/> <classpathentry kind="output" path="bin"/> <classpathentry exported="true" kind="lib" path="lib/commons-logging-1.2.jar"/> </classpath>
kind没有 folder 之类的?
classpathentry
classpathentry add path
加一个src,表示当前目录试试
<classpathentry kind="src" path="."/>
结果报错:
Cannot nest 'analysis/src/main/java' inside 'analysis'. To enable the nesting exclude 'src/' from 'analysis' The project cannot be built until build path errors are resolved
加:
<classpathentry kind="src" path="log4j2.xml"/>
没报错。
去运行试试,结果没用。
换用lib
<classpathentry kind="lib" path="log4j2.xml"/>
结果:
Archive for required library: 'log4j2.xml' in project 'analysis' cannot be read or is not a valid ZIP file
换用:
<classpathentry kind="src" path="./log4j2.xml"/>
结果:问题依旧。
<classpathentry kind="src" path="src"/>
报错:
Cannot nest 'analysis/src/main/java' inside 'analysis/src'. To enable the nesting exclude 'main/' from 'analysis/src'
【总结】
此处,想要实现官网
“By default, Log4j looks for a configuration file named log4j2.xml (not log4j.xml) in the classpath.”
说的,即把 log4j2.xml 加到此处 VSCode中maven的classpath中
已经从官网
中得知,虽然之前看到解释说 .classpath 只是Eclipse的(JDT插件)的配置文件,但是此处VSCode也支持
所以想要从 .classpath 中加 log4j2.xml 到classpath中。
但是:最终失败了。不知道如何才能实现官网说的
add log4j2.xml to classpath
【后记】
看到
逻辑上,是可以用additionalClasspathElements去加classpath的
有空再去试试