aboutsummaryrefslogtreecommitdiffstats
path: root/controller-api/src/main/java/com/yahoo/vespa/hosted/controller/api/integration/LogEntry.java
blob: 96a0e5baa561508778f0d17afcd29d1673da2268 (plain) (blame)
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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.hosted.controller.api.integration;

import com.yahoo.log.LogLevel;

import java.util.logging.Level;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UncheckedIOException;
import java.time.Instant;
import java.time.temporal.ChronoUnit;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

import static java.nio.charset.StandardCharsets.UTF_8;
import static java.util.Objects.requireNonNull;

/**
 * Immutable, simple log entries.
 *
 * @author jonmv
 */
public class LogEntry {

    private final long id;
    private final Instant at;
    private final Type type;
    private final String message;

    public LogEntry(long id, Instant at, Type type, String message) {
        if (id < 0)
            throw new IllegalArgumentException("Id must be non-negative, but was " + id + ".");

        this.id = id;
        this.at = at;
        this.type = requireNonNull(type);
        this.message = requireNonNull(message);
    }

    public long id() {
        return id;
    }

    public Instant at() {
        return at;
    }

    public Type type() {
        return type;
    }

    public String message() {
        return message;
    }

    @SuppressWarnings("deprecation")
    public static List<LogEntry> parseVespaLog(InputStream log, Instant from) {
        try (BufferedReader reader = new BufferedReader(new InputStreamReader(log, UTF_8))) {
            return reader.lines()
                         .map(line -> line.split("\t"))
                         .filter(parts -> parts.length == 7)
                         .map(parts -> new LogEntry(0,
                                                    Instant.EPOCH.plus((long) (Double.parseDouble(parts[0]) * 1_000_000), ChronoUnit.MICROS),
                                                    typeOf(LogLevel.parse(parts[5])),
                                                    parts[1] + '\t' + parts[3] + '\t' + parts[4] + '\n' +
                                                    parts[6].replaceAll("\\\\n", "\n")
                                                            .replaceAll("\\\\t", "\t")))
                         .filter(entry -> entry.at().isAfter(from))
                         .limit(100_000)
                         .toList();
        }
        catch (IOException e) {
            throw new UncheckedIOException(e);
        }
    }


    @Override
    public String toString() {
        return "LogEntry{" +
               "id=" + id +
               ", at=" + at.toEpochMilli() +
               ", type=" + type +
               ", message='" + message + '\'' +
               '}';
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof LogEntry)) return false;
        LogEntry entry = (LogEntry) o;
        return id == entry.id &&
               at.toEpochMilli() == entry.at.toEpochMilli() &&
               type == entry.type &&
               Objects.equals(message, entry.message);
    }

    @Override
    public int hashCode() {
        return Objects.hash(id, at, type, message);
    }

    @SuppressWarnings("deprecation")
    public static Type typeOf(Level level) {
        return    level.intValue() < Level.INFO.intValue() || level.intValue() == LogLevel.IntValEVENT ? Type.debug
                : level.intValue() < Level.WARNING.intValue() ? Type.info
                : level.intValue() < Level.SEVERE.intValue() ? Type.warning
                : Type.error;
    }


    /** The type of entry, used for rendering. */
    public enum Type {
        debug, info, warning, error, html;
    }

}