|
| 1 | +/* |
| 2 | + * Copyright 2025 Typelevel |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.typelevel.otel4s.logs |
| 18 | + |
| 19 | +import cats.Hash |
| 20 | +import cats.Show |
| 21 | + |
| 22 | +/** Represents the severity of a log record. |
| 23 | + * |
| 24 | + * @param value |
| 25 | + * smaller numerical values correspond to less severe events (such as debug events), larger numerical values |
| 26 | + * correspond to more severe events (such as errors and critical events) |
| 27 | + * |
| 28 | + * @param name |
| 29 | + * a human-readable name, see [[https://opentelemetry.io/docs/specs/otel/logs/data-model/#displaying-severity]] |
| 30 | + * |
| 31 | + * @see |
| 32 | + * [[https://opentelemetry.io/docs/specs/otel/logs/data-model/#field-severitynumber]] |
| 33 | + */ |
| 34 | +sealed abstract class Severity(val value: Int, val name: String) { |
| 35 | + |
| 36 | + override final def hashCode(): Int = |
| 37 | + Hash[Severity].hash(this) |
| 38 | + |
| 39 | + override final def equals(obj: Any): Boolean = |
| 40 | + obj match { |
| 41 | + case other: Severity => Hash[Severity].eqv(this, other) |
| 42 | + case _ => false |
| 43 | + } |
| 44 | + |
| 45 | + override final def toString: String = |
| 46 | + Show[Severity].show(this) |
| 47 | + |
| 48 | +} |
| 49 | + |
| 50 | +object Severity { |
| 51 | + |
| 52 | + /** A fine-grained debugging event. Typically disabled in default configurations. |
| 53 | + */ |
| 54 | + sealed abstract class Trace(value: Int, name: String) extends Severity(value, name) |
| 55 | + |
| 56 | + /** A debugging event. |
| 57 | + */ |
| 58 | + sealed abstract class Debug(value: Int, name: String) extends Severity(value, name) |
| 59 | + |
| 60 | + /** An informational event. Indicates that an event happened. |
| 61 | + */ |
| 62 | + sealed abstract class Info(value: Int, name: String) extends Severity(value, name) |
| 63 | + |
| 64 | + /** A warning event. Not an error but is likely more important than an informational event. |
| 65 | + */ |
| 66 | + sealed abstract class Warn(value: Int, name: String) extends Severity(value, name) |
| 67 | + |
| 68 | + /** An error event. Something went wrong. |
| 69 | + */ |
| 70 | + sealed abstract class Error(value: Int, name: String) extends Severity(value, name) |
| 71 | + |
| 72 | + /** A fatal error such as application or system crash. |
| 73 | + */ |
| 74 | + sealed abstract class Fatal(value: Int, name: String) extends Severity(value, name) |
| 75 | + |
| 76 | + /** A fine-grained debugging event. Typically disabled in default configurations. |
| 77 | + */ |
| 78 | + def trace: Trace = Trace1 |
| 79 | + def trace2: Trace = Trace2 |
| 80 | + def trace3: Trace = Trace3 |
| 81 | + def trace4: Trace = Trace4 |
| 82 | + |
| 83 | + /** A debugging event. |
| 84 | + */ |
| 85 | + def debug: Debug = Debug1 |
| 86 | + def debug2: Debug = Debug2 |
| 87 | + def debug3: Debug = Debug3 |
| 88 | + def debug4: Debug = Debug4 |
| 89 | + |
| 90 | + /** An informational event. Indicates that an event happened. |
| 91 | + */ |
| 92 | + def info: Info = Info1 |
| 93 | + def info2: Info = Info2 |
| 94 | + def info3: Info = Info3 |
| 95 | + def info4: Info = Info4 |
| 96 | + |
| 97 | + /** A warning event. Not an error but is likely more important than an informational event. |
| 98 | + */ |
| 99 | + def warn: Warn = Warn1 |
| 100 | + def warn2: Warn = Warn2 |
| 101 | + def warn3: Warn = Warn3 |
| 102 | + def warn4: Warn = Warn4 |
| 103 | + |
| 104 | + /** An error event. Something went wrong. |
| 105 | + */ |
| 106 | + def error: Error = Error1 |
| 107 | + def error2: Error = Error2 |
| 108 | + def error3: Error = Error3 |
| 109 | + def error4: Error = Error4 |
| 110 | + |
| 111 | + /** A fatal error such as application or system crash. |
| 112 | + */ |
| 113 | + def fatal: Fatal = Fatal1 |
| 114 | + def fatal2: Fatal = Fatal2 |
| 115 | + def fatal3: Fatal = Fatal3 |
| 116 | + def fatal4: Fatal = Fatal4 |
| 117 | + |
| 118 | + implicit val severityHash: Hash[Severity] = Hash.by(_.value) |
| 119 | + |
| 120 | + /** @see |
| 121 | + * [[https://opentelemetry.io/docs/specs/otel/logs/data-model/#displaying-severity]] |
| 122 | + */ |
| 123 | + implicit val severityShow: Show[Severity] = Show.show(_.name) |
| 124 | + |
| 125 | + private[otel4s] case object Trace1 extends Trace(1, "TRACE") |
| 126 | + private[otel4s] case object Trace2 extends Trace(2, "TRACE2") |
| 127 | + private[otel4s] case object Trace3 extends Trace(3, "TRACE3") |
| 128 | + private[otel4s] case object Trace4 extends Trace(4, "TRACE4") |
| 129 | + |
| 130 | + private[otel4s] case object Debug1 extends Debug(5, "DEBUG") |
| 131 | + private[otel4s] case object Debug2 extends Debug(6, "DEBUG2") |
| 132 | + private[otel4s] case object Debug3 extends Debug(7, "DEBUG3") |
| 133 | + private[otel4s] case object Debug4 extends Debug(8, "DEBUG4") |
| 134 | + |
| 135 | + private[otel4s] case object Warn1 extends Warn(13, "WARN") |
| 136 | + private[otel4s] case object Warn2 extends Warn(14, "WARN2") |
| 137 | + private[otel4s] case object Warn3 extends Warn(15, "WARN3") |
| 138 | + private[otel4s] case object Warn4 extends Warn(16, "WARN4") |
| 139 | + |
| 140 | + private[otel4s] case object Info1 extends Info(9, "INFO") |
| 141 | + private[otel4s] case object Info2 extends Info(10, "INFO2") |
| 142 | + private[otel4s] case object Info3 extends Info(11, "INFO3") |
| 143 | + private[otel4s] case object Info4 extends Info(12, "INFO4") |
| 144 | + |
| 145 | + private[otel4s] case object Error1 extends Error(17, "ERROR") |
| 146 | + private[otel4s] case object Error2 extends Error(18, "ERROR2") |
| 147 | + private[otel4s] case object Error3 extends Error(19, "ERROR3") |
| 148 | + private[otel4s] case object Error4 extends Error(20, "ERROR4") |
| 149 | + |
| 150 | + private[otel4s] case object Fatal1 extends Fatal(21, "FATAL") |
| 151 | + private[otel4s] case object Fatal2 extends Fatal(22, "FATAL2") |
| 152 | + private[otel4s] case object Fatal3 extends Fatal(23, "FATAL3") |
| 153 | + private[otel4s] case object Fatal4 extends Fatal(24, "FATAL4") |
| 154 | +} |
0 commit comments