summaryrefslogtreecommitdiffstats
path: root/controller-api/src/main/java/com/yahoo/vespa/hosted/controller/api/integration/LogEntry.java
blob: 3da6b34542c7740269d3d48aab060731bfb526a9 (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
// Copyright 2018 Yahoo Holdings. 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.Objects;
import java.util.logging.Level;

import static java.util.Objects.requireNonNull;

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

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

    public LogEntry(long id, long 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 long at() {
        return at;
    }

    public Type type() {
        return type;
    }

    public String message() {
        return message;
    }

    @Override
    public String toString() {
        return "LogEntry{" +
               "id=" + id +
               ", at=" + at +
               ", 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 == entry.at &&
               type == entry.type &&
               Objects.equals(message, entry.message);
    }

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

    public static Type typeOf(Level level) {
        return    level.intValue() < LogLevel.INFO.intValue() ? Type.debug
                : level.intValue() < LogLevel.WARNING.intValue() ? Type.info
                : level.intValue() < LogLevel.ERROR.intValue() ? Type.warning
                : Type.error;
    }

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

}