最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

【未解决】VSCode中把log4j加到maven的classpath中

maven crifan 2268浏览 0评论
折腾:
【未解决】java的log4j2中没有生成File类型的log文件
期间,感觉是:需要把log4j的classpath加到maven中。
log4j2 add xml to classpath
java – log4j 2 – configuration issue – Stack Overflow
java – Using a XML File (log4j2.xml) to configure Log4j 2 – Stack Overflow
java – How to adding log4j2.xml to classpath in Netbeans maven project – Stack Overflow
maven  classpath classpathentry
maven .classpath classpathentry
maven add   classpathentry
Maven does not add classpath to Eclipse project – Stack Overflow
build – Maven – how can I add an arbitrary classpath entry to a jar? – Stack Overflow
java .classpath file
Setting the class path
java – What’s in an Eclipse .classpath/.project file? – Stack Overflow
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
Apache Maven Archiver – Set Up The Classpath
感觉算是把 log4j2.xml,加到maven中
If using maven, usually you put log4j.properties under java or resources? – Stack Overflow
java – Where does maven search for log4j.properties file? – Stack Overflow
java – Log4j2 and maven – Picking wrong config file – Stack Overflow
Log4j – Frequently Asked Questions
java – Specifying Log4j2 Configuration File When Using Executable JAR – Stack Overflow
log4j – How can I change the default location of log4j2.xml in Java Spring Boot? – Stack Overflow
Log4j – Configuring Log4j 2
Log4j2 xml configuration example – HowToDoInJava
一种方式是:
放到:
src/main/resources/
中:
src/main/resources/log4j2.xml
但是此处不想这么放,想要搞懂,如何指定classpath,把log4j2.xml加进去
Spring boot log4j2.xml example – HowToDoInJava
“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
https://mkyong.com/logging/apache-log4j-2-tutorials/
说是放到:
src/resources/log4j2.xml
或:
src/resources/log4j2.xml
maven add log4j2.xml to classpath
log4j2入门hello world – 掘金
如果不用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
ERROR StatusLogger No log4j2 configuration file found. Using default configuration: logging only errors to the console • Crunchify
“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
Java: How to configure log4j.properties correctly – log4j Sample Program • Crunchify
vscode maven log4j2.xml location
java – No log4j2 configuration file found. Using default configuration: logging only errors to the console – Stack Overflow
java – log4j2.xml path Configuration – Stack Overflow
Log4j – Frequently Asked Questions
里面没有明说,必须是: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
java – Using pom.xml SystemProperty in log4j2 configuration file – Stack Overflow
maven – Unable to define custom Log4j2 configuration file in pom.xml – Stack Overflow
java – Using a XML File (log4j2.xml) to configure Log4j 2 – Stack Overflow
试试加配置到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
}
log4j2 with log4j2.xml Configuration Example
也说了,必须是:
+--src
|   +--main
|       +--java
|           +--com
|               +--memorynotfound
|                   +--logging
|                       |--Log4j2Example.java
|       +--resources
|           |--log4j2.xml
pom.xml
这个结构
java – How to adding log4j2.xml to classpath in Netbeans maven project – Stack Overflow
Log4j – Configuring Log4j 2
“ 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
Maven Support, Java Package, and Dependency Management in Visual Studio Code
“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
Setting the classpath in a Visual Studio Code Maven-based Java project – Stack Overflow
” <classpathentry kind=”lib” path=”/opt/ABBYY/FREngine12/Inc/Java/com.abbyy.FREngine.jar” />”
Visual Studio Code, Java Extension, howto add jar to classpath – Stack Overflow
<?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 之类的?
Visual Studio Code – Java Classpath is incomplete. Only syntax errors will be reported – Stack Overflow
java – Can’t use external jar in VS Code – Stack Overflow
Wrong classpath at runtime when the maven project is in a folder · Issue #584 · microsoft/vscode-java-debug
classpathentry
classpathentry add path
eclipse项目中.classpath文件详解_eclipse,myeclipse,classpath_pengmm1990的博客-CSDN博客
加一个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"/>
没报错。
去运行试试,结果没用。
MyBatis Generator Core – The <classPathEntry> Element
ClasspathEntry (Eclipse Platform API Specification)
java – Include multiple jars with classpathentry – Stack Overflow
换用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"/>
结果:问题依旧。
java – Use Eclipse classpath variable to replace absolute “sourcepath” path? – Stack Overflow
Eclipse Classpath Editing — eclim (eclipse + vim)
<classpathentry kind="src" path="src"/>
报错:
Cannot nest 'analysis/src/main/java' inside 'analysis/src'. To enable the nesting exclude 'main/' from 'analysis/src'
【总结】
此处,想要实现官网
Log4j – Frequently Asked Questions
“By default, Log4j looks for a configuration file named log4j2.xml (not log4j.xml) in the classpath.”
说的,即把 log4j2.xml 加到此处 VSCode中maven的classpath中
已经从官网
Maven Support, Java Package, and Dependency Management in Visual Studio Code
中得知,虽然之前看到解释说 .classpath 只是Eclipse的(JDT插件)的配置文件,但是此处VSCode也支持
所以想要从  .classpath  中加 log4j2.xml 到classpath中。
但是:最终失败了。不知道如何才能实现官网说的
add log4j2.xml to classpath
【后记】
看到
Apache Maven Archiver – Set Up The Classpath
Configuring the Classpath
逻辑上,是可以用additionalClasspathElements去加classpath的
有空再去试试

转载请注明:在路上 » 【未解决】VSCode中把log4j加到maven的classpath中

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
87 queries in 0.189 seconds, using 22.05MB memory