View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.logging;
20  
21  import org.apache.maven.slf4j.MavenSimpleLogger;
22  import org.slf4j.MDC;
23  
24  /**
25   * Forwards log messages to the client.
26   */
27  public class ProjectBuildLogAppender implements AutoCloseable {
28  
29      private static final String KEY_PROJECT_ID = "maven.project.id";
30      private static final ThreadLocal<String> PROJECT_ID = new InheritableThreadLocal<>();
31      private static final ThreadLocal<String> FORKING_PROJECT_ID = new InheritableThreadLocal<>();
32  
33      public static String getProjectId() {
34          return PROJECT_ID.get();
35      }
36  
37      public static void setProjectId(String projectId) {
38          String forkingProjectId = FORKING_PROJECT_ID.get();
39          if (forkingProjectId != null) {
40              if (projectId != null) {
41                  projectId = forkingProjectId + "/" + projectId;
42              } else {
43                  projectId = forkingProjectId;
44              }
45          }
46          if (projectId != null) {
47              PROJECT_ID.set(projectId);
48              MDC.put(KEY_PROJECT_ID, projectId);
49          } else {
50              PROJECT_ID.remove();
51              MDC.remove(KEY_PROJECT_ID);
52          }
53      }
54  
55      public static void setForkingProjectId(String forkingProjectId) {
56          if (forkingProjectId != null) {
57              FORKING_PROJECT_ID.set(forkingProjectId);
58          } else {
59              FORKING_PROJECT_ID.remove();
60          }
61      }
62  
63      public static void updateMdc() {
64          String id = getProjectId();
65          if (id != null) {
66              MDC.put(KEY_PROJECT_ID, id);
67          } else {
68              MDC.remove(KEY_PROJECT_ID);
69          }
70      }
71  
72      private final BuildEventListener buildEventListener;
73  
74      public ProjectBuildLogAppender(BuildEventListener buildEventListener) {
75          this.buildEventListener = buildEventListener;
76          MavenSimpleLogger.setLogSink(this::accept);
77      }
78  
79      protected void accept(String message) {
80          String projectId = MDC.get(KEY_PROJECT_ID);
81          buildEventListener.projectLogMessage(projectId, message);
82      }
83  
84      @Override
85      public void close() throws Exception {
86          MavenSimpleLogger.setLogSink(null);
87      }
88  }