aboutsummaryrefslogtreecommitdiffstats
path: root/server/sonar-process/src/main/java/org/sonar/process/logging/LogbackJsonLayout.java
blob: ff37db38cf8df3345cb50a838730144cca1eefab (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/*
 * SonarQube
 * Copyright (C) 2009-2025 SonarSource SA
 * mailto:info AT sonarsource DOT com
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
package org.sonar.process.logging;

import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.classic.spi.IThrowableProxy;
import ch.qos.logback.classic.spi.StackTraceElementProxy;
import ch.qos.logback.core.CoreConstants;
import ch.qos.logback.core.LayoutBase;
import com.google.gson.stream.JsonWriter;
import java.io.IOException;
import java.io.StringWriter;
import java.time.Instant;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.regex.Pattern;
import org.apache.commons.lang3.StringUtils;

import static java.lang.String.format;
import static java.util.Objects.requireNonNull;

/**
 * Formats logs in JSON.
 * <p>
 * Strongly inspired by https://github.com/qos-ch/logback/blob/master/logback-classic/src/main/java/ch/qos/logback/classic/html/DefaultThrowableRenderer.java
 */
public class LogbackJsonLayout extends LayoutBase<ILoggingEvent> {

  static final DateTimeFormatter DATE_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSXXX")
    .withLocale(Locale.US)
    .withZone(ZoneId.systemDefault());
  private static final Pattern NEWLINE_REGEXP = Pattern.compile("\n");

  private final String processKey;
  private final String nodeName;
  private final List<String> exclusions;

  public LogbackJsonLayout(String processKey, String nodeName) {
    this(processKey, nodeName, List.of());
  }

  public LogbackJsonLayout(String processKey, String nodeName, List<String> exclusions) {
    this.processKey = requireNonNull(processKey);
    this.nodeName = nodeName;
    this.exclusions = exclusions;
  }

  String getProcessKey() {
    return processKey;
  }

  @Override
  public String doLayout(ILoggingEvent event) {
    StringWriter output = new StringWriter();
    try (JsonWriter json = new JsonWriter(output)) {
      json.beginObject();
      if (!"".equals(nodeName)) {
        json.name("nodename").value(nodeName);
      }
      json.name("process").value(processKey);
      for (Map.Entry<String, String> entry : event.getMDCPropertyMap().entrySet()) {
        if (entry.getValue() != null && !exclusions.contains(entry.getKey())) {
          json.name(entry.getKey()).value(entry.getValue());
        }
      }
      json
        .name("timestamp").value(DATE_FORMATTER.format(Instant.ofEpochMilli(event.getTimeStamp())))
        .name("severity").value(event.getLevel().toString())
        .name("logger").value(event.getLoggerName())
        .name("message").value(NEWLINE_REGEXP.matcher(event.getFormattedMessage()).replaceAll("\r"));
      IThrowableProxy tp = event.getThrowableProxy();
      if (tp != null) {
        json.name("stacktrace").beginArray();
        int nbOfTabs = 0;
        while (tp != null) {
          printFirstLine(json, tp, nbOfTabs);
          render(json, tp, nbOfTabs);
          tp = tp.getCause();
          nbOfTabs++;
        }
        json.endArray();

      }
      json.endObject();
    } catch (Exception e) {
      e.printStackTrace();
      throw new IllegalStateException("BUG - fail to create JSON", e);
    }
    output.write(System.lineSeparator());
    return output.toString();
  }

  private static void render(JsonWriter output, IThrowableProxy tp, int nbOfTabs) throws IOException {
    String tabs = StringUtils.repeat("\t", nbOfTabs);
    int commonFrames = tp.getCommonFrames();
    StackTraceElementProxy[] stepArray = tp.getStackTraceElementProxyArray();

    for (int i = 0; i < stepArray.length - commonFrames; i++) {
      StackTraceElementProxy step = stepArray[i];
      output.value(tabs + step.toString());
    }

    if (commonFrames > 0) {
      output.value(tabs + "... " + commonFrames + " common frames omitted");
    }

    for (IThrowableProxy suppressed : tp.getSuppressed()) {
      output.value(format("%sSuppressed: %s: %s", tabs, suppressed.getClassName(), suppressed.getMessage()));
      render(output, suppressed, nbOfTabs + 1);
    }
  }

  private static void printFirstLine(JsonWriter output, IThrowableProxy tp, int nbOfTabs) throws IOException {
    String tabs = StringUtils.repeat("\t", nbOfTabs);
    int commonFrames = tp.getCommonFrames();
    if (commonFrames > 0) {
      output.value(tabs + CoreConstants.CAUSED_BY);
    }
    output.value(tabs + tp.getClassName() + ": " + tp.getMessage());
  }
}