Eclipse:为Maven日志条目添加时间戳

时间:2016-11-23 14:15:42

标签: java eclipse maven logging console

我正在使用Eclipse Neon.1并使用Google App Engine插件的deploy目标

<!-- https://github.com/GoogleCloudPlatform/app-maven-plugin -->
<plugin>
    <groupId>com.google.cloud.tools</groupId>
    <artifactId>appengine-maven-plugin</artifactId>
    <version>0.1.2</version>
    <configuration>
        <deploy.project>${app.id}</deploy.project>
        <deploy.version>${app.version}</deploy.version>
    </configuration>
</plugin>

这是我的启动配置

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<launchConfiguration type="org.eclipse.m2e.Maven2LaunchConfigurationType">
    <booleanAttribute key="M2_DEBUG_OUTPUT" value="true" />
    <stringAttribute key="M2_GOALS" value="com.google.cloud.tools:appengine-maven-plugin:deploy" />
    <booleanAttribute key="M2_NON_RECURSIVE" value="false" />
    <booleanAttribute key="M2_OFFLINE" value="false" />
    <stringAttribute key="M2_PROFILES" value="" />
    <listAttribute key="M2_PROPERTIES" />
    <stringAttribute key="M2_RUNTIME" value="EMBEDDED" />
    <booleanAttribute key="M2_SKIP_TESTS" value="true" />
    <intAttribute key="M2_THREADS" value="1" />
    <booleanAttribute key="M2_UPDATE_SNAPSHOTS" value="false" />
    <stringAttribute key="M2_USER_SETTINGS" value="" />
    <booleanAttribute key="M2_WORKSPACE_RESOLUTION" value="false" />
    <stringAttribute key="org.eclipse.jdt.launching.WORKING_DIRECTORY" value="${project_loc}" />
</launchConfiguration>

我启用了debug_output以获得完整的日志,这里有几行直接来自Maven Build

[DEBUG]   Included: org.apache.maven.plugins:maven-resources-plugin:jar:2.6
[DEBUG]   Included: org.apache.maven.reporting:maven-reporting-api:jar:2.0.6
[DEBUG]   Included: org.apache.maven.doxia:doxia-sink-api:jar:1.0-alpha-7
[DEBUG]   Included: commons-cli:commons-cli:jar:1.0

以下是特定GCloud目标的几行

[INFO] GCLOUD: Reading application configuration data...
[INFO] GCLOUD: nov 23, 2016 3:04:54 PM
[INFO] GCLOUD: Configuration Warning : <application>/<version> XML elements and --application/--version should not be specified when staging
[INFO] GCLOUD: 
[INFO] GCLOUD: The following parameters will be scrubbed from app.yaml

有没有办法在每行前面加上时间戳? 对于这种请求,我认为这是Eclipse接口的特定配置,并不是特定于Maven。

我已经找到了与此主题相关的其他问题,但是所有与maven直接从mvn命令行启动的问题都有关,而不是使用Eclipse内置控制台。

没有任何其他配置,我所拥有的唯一“临时”数据是在整个构建的最后,有这样的日志:

[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 15:52 min
[INFO] Finished at: 2016-11-23T14:57:19+01:00
[INFO] Final Memory: 37M/314M
[INFO] ------------------------------------------------------------------------

预期的输出是这样的

[2016-11-23T14:57:19+01:00][DEBUG]   Included: org.apache.maven.plugins:maven-resources-plugin:jar:2.6
[2016-11-23T14:57:20+01:00][DEBUG]   Included: org.apache.maven.reporting:maven-reporting-api:jar:2.0.6
[2016-11-23T14:57:21+01:00][DEBUG]   Included: org.apache.maven.doxia:doxia-sink-api:jar:1.0-alpha-7
[2016-11-23T14:57:22+01:00][DEBUG]   Included: commons-cli:commons-cli:jar:1.0
....
[2016-11-23T14:59:22+01:00][INFO] GCLOUD: Reading application configuration data...
[2016-11-23T14:59:23+01:00][INFO] GCLOUD: nov 23, 2016 3:04:54 PM
[2016-11-23T14:59:24+01:00][INFO] GCLOUD: Configuration Warning : <application>/<version> XML elements and --application/--version should not be specified when staging
[2016-11-23T14:59:25+01:00][INFO] GCLOUD: 
[2016-11-23T14:59:26+01:00][INFO] GCLOUD: The following parameters will be scrubbed from app.yaml

2 个答案:

答案 0 :(得分:14)

使用Maven 3.5.0,您可以配置日志记录输出,以便为每一行添加时间戳。只需执行以下操作:

$MAVEN_HOME/conf/logging/simplelogger.properties设置org.slf4j.simpleLogger.showDateTime=true并添加模式属性org.slf4j.simpleLogger.dateTimeFormat=HH:mm:ss,SSS

这将产生如下输出:

10:28:25.849 [INFO] Scanning for projects...
10:28:25.914 [INFO]
...
10:28:25.975 [INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ xxx ---
10:28:26.023 [INFO] Deleting /home/shillner/samples/xxx/target
10:28:26.024 [INFO]
...

注意:此解决方案仅适用于Maven 3.1.0及更高版本,因为此版本中的日志记录已迁移到slf4j。

希望这有所帮助,虽然有点晚了;)

答案 1 :(得分:1)

您可以通过JVM属性指定选项。例如:

mvn clean install -Dorg.slf4j.simpleLogger.showDateTime=true -Dorg.slf4j.simpleLogger.dateTimeFormat=HH:mm:ss,SSS