|
| 1 | +package pl.mjaron.tinyloki; |
| 2 | + |
| 3 | +/** |
| 4 | + * A set of streams with different log level label values but common other labels. |
| 5 | + * |
| 6 | + * @since 1.1.1 |
| 7 | + */ |
| 8 | +public class StreamSet { |
| 9 | + |
| 10 | + private final TinyLoki loki; |
| 11 | + private final Labels labels; |
| 12 | + |
| 13 | + private ILogStream fatalStream; |
| 14 | + private ILogStream warningStream; |
| 15 | + private ILogStream infoStream; |
| 16 | + private ILogStream debugStream; |
| 17 | + private ILogStream traceStream; |
| 18 | + private ILogStream unknownStream; |
| 19 | + |
| 20 | + StreamSet(final TinyLoki loki, final Labels labels) { |
| 21 | + this.loki = loki; |
| 22 | + this.labels = labels; |
| 23 | + } |
| 24 | + |
| 25 | + /** |
| 26 | + * Releases all internal streams. |
| 27 | + * |
| 28 | + * @since 1.1.1 |
| 29 | + */ |
| 30 | + synchronized void release() { |
| 31 | + if (fatalStream != null) { |
| 32 | + fatalStream.release(); |
| 33 | + } |
| 34 | + if (warningStream != null) { |
| 35 | + warningStream.release(); |
| 36 | + } |
| 37 | + if (infoStream != null) { |
| 38 | + infoStream.release(); |
| 39 | + } |
| 40 | + if (debugStream != null) { |
| 41 | + debugStream.release(); |
| 42 | + } |
| 43 | + if (traceStream != null) { |
| 44 | + traceStream.release(); |
| 45 | + } |
| 46 | + if (unknownStream != null) { |
| 47 | + unknownStream.release(); |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * Provides a stream with common labels and requested log level. |
| 53 | + * |
| 54 | + * @return Stream with {@link Labels#FATAL} log level. |
| 55 | + * @since 1.1.1 |
| 56 | + */ |
| 57 | + synchronized ILogStream fatal() { |
| 58 | + if (fatalStream == null) { |
| 59 | + fatalStream = loki.stream().fatal().l(labels).open(); |
| 60 | + } |
| 61 | + return fatalStream; |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * Provides a stream with common labels and requested log level. |
| 66 | + * |
| 67 | + * @return Stream with {@link Labels#WARN} log level. |
| 68 | + * @since 1.1.1 |
| 69 | + */ |
| 70 | + synchronized ILogStream warning() { |
| 71 | + if (warningStream == null) { |
| 72 | + warningStream = loki.stream().warning().l(labels).open(); |
| 73 | + } |
| 74 | + return warningStream; |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * Provides a stream with common labels and requested log level. |
| 79 | + * |
| 80 | + * @return Stream with {@link Labels#INFO} log level. |
| 81 | + * @since 1.1.1 |
| 82 | + */ |
| 83 | + synchronized ILogStream info() { |
| 84 | + if (infoStream == null) { |
| 85 | + infoStream = loki.stream().info().l(labels).open(); |
| 86 | + } |
| 87 | + return infoStream; |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * Provides a stream with common labels and requested log level. |
| 92 | + * |
| 93 | + * @return Stream with {@link Labels#DEBUG} log level. |
| 94 | + * @since 1.1.1 |
| 95 | + */ |
| 96 | + synchronized ILogStream debug() { |
| 97 | + if (debugStream == null) { |
| 98 | + debugStream = loki.stream().debug().l(labels).open(); |
| 99 | + } |
| 100 | + return debugStream; |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * Provides a stream with common labels and requested log level. |
| 105 | + * |
| 106 | + * @return Stream with {@link Labels#VERBOSE} log level. |
| 107 | + * @since 1.1.1 |
| 108 | + */ |
| 109 | + synchronized ILogStream verbose() { |
| 110 | + if (traceStream == null) { |
| 111 | + traceStream = loki.stream().trace().l(labels).open(); |
| 112 | + } |
| 113 | + return traceStream; |
| 114 | + } |
| 115 | + |
| 116 | + /** |
| 117 | + * Provides a stream with common labels and requested log level. |
| 118 | + * |
| 119 | + * @return Stream with {@link Labels#UNKNOWN} log level. |
| 120 | + * @since 1.1.1 |
| 121 | + */ |
| 122 | + synchronized ILogStream unknown() { |
| 123 | + if (unknownStream == null) { |
| 124 | + unknownStream = loki.stream().unknown().l(labels).open(); |
| 125 | + } |
| 126 | + return unknownStream; |
| 127 | + } |
| 128 | + |
| 129 | + /** |
| 130 | + * Writes the log with common labels and {@link Labels#FATAL} log level. |
| 131 | + * |
| 132 | + * @param line Log message. |
| 133 | + * @since 1.1.1 |
| 134 | + */ |
| 135 | + void fatal(final String line) { |
| 136 | + fatal().log(line); |
| 137 | + } |
| 138 | + |
| 139 | + /** |
| 140 | + * Writes the log with common labels and {@link Labels#FATAL} log level. |
| 141 | + * |
| 142 | + * @param line Log message. |
| 143 | + * @param structuredMetadata Labels related to single log line only. |
| 144 | + * @since 1.1.1 |
| 145 | + */ |
| 146 | + void fatal(final String line, final Labels structuredMetadata) { |
| 147 | + fatal().log(line, structuredMetadata); |
| 148 | + } |
| 149 | + |
| 150 | + /** |
| 151 | + * Writes the log with common labels and {@link Labels#WARN} log level. |
| 152 | + * |
| 153 | + * @param line Log message. |
| 154 | + * @since 1.1.1 |
| 155 | + */ |
| 156 | + void warning(final String line) { |
| 157 | + warning().log(line); |
| 158 | + } |
| 159 | + |
| 160 | + /** |
| 161 | + * Writes the log with common labels and {@link Labels#WARN} log level. |
| 162 | + * |
| 163 | + * @param line Log message. |
| 164 | + * @param structuredMetadata Labels related to single log line only. |
| 165 | + * @since 1.1.1 |
| 166 | + */ |
| 167 | + void warning(final String line, final Labels structuredMetadata) { |
| 168 | + warning().log(line, structuredMetadata); |
| 169 | + } |
| 170 | + |
| 171 | + /** |
| 172 | + * Writes the log with common labels and {@link Labels#INFO} log level. |
| 173 | + * |
| 174 | + * @param line Log message. |
| 175 | + * @since 1.1.1 |
| 176 | + */ |
| 177 | + void info(final String line) { |
| 178 | + info().log(line); |
| 179 | + } |
| 180 | + |
| 181 | + /** |
| 182 | + * Writes the log with common labels and {@link Labels#INFO} log level. |
| 183 | + * |
| 184 | + * @param line Log message. |
| 185 | + * @param structuredMetadata Labels related to single log line only. |
| 186 | + * @since 1.1.1 |
| 187 | + */ |
| 188 | + void info(final String line, final Labels structuredMetadata) { |
| 189 | + info().log(line, structuredMetadata); |
| 190 | + } |
| 191 | + |
| 192 | + /** |
| 193 | + * Writes the log with common labels and {@link Labels#DEBUG} log level. |
| 194 | + * |
| 195 | + * @param line Log message. |
| 196 | + * @since 1.1.1 |
| 197 | + */ |
| 198 | + void debug(final String line) { |
| 199 | + debug().log(line); |
| 200 | + } |
| 201 | + |
| 202 | + /** |
| 203 | + * Writes the log with common labels and {@link Labels#DEBUG} log level. |
| 204 | + * |
| 205 | + * @param line Log message. |
| 206 | + * @param structuredMetadata Labels related to single log line only. |
| 207 | + * @since 1.1.1 |
| 208 | + */ |
| 209 | + void debug(final String line, final Labels structuredMetadata) { |
| 210 | + debug().log(line, structuredMetadata); |
| 211 | + } |
| 212 | + |
| 213 | + /** |
| 214 | + * Writes the log with common labels and {@link Labels#VERBOSE} log level. |
| 215 | + * |
| 216 | + * @param line Log message. |
| 217 | + * @since 1.1.1 |
| 218 | + */ |
| 219 | + void verbose(final String line) { |
| 220 | + verbose().log(line); |
| 221 | + } |
| 222 | + |
| 223 | + /** |
| 224 | + * Writes the log with common labels and {@link Labels#VERBOSE} log level. |
| 225 | + * |
| 226 | + * @param line Log message. |
| 227 | + * @param structuredMetadata Labels related to single log line only. |
| 228 | + * @since 1.1.1 |
| 229 | + */ |
| 230 | + void verbose(final String line, final Labels structuredMetadata) { |
| 231 | + verbose().log(line, structuredMetadata); |
| 232 | + } |
| 233 | + |
| 234 | + /** |
| 235 | + * Writes the log with common labels and {@link Labels#UNKNOWN} log level. |
| 236 | + * |
| 237 | + * @param line Log message. |
| 238 | + * @since 1.1.1 |
| 239 | + */ |
| 240 | + void unknown(final String line) { |
| 241 | + unknown().log(line); |
| 242 | + } |
| 243 | + |
| 244 | + /** |
| 245 | + * Writes the log with common labels and {@link Labels#UNKNOWN} log level. |
| 246 | + * |
| 247 | + * @param line Log message. |
| 248 | + * @param structuredMetadata Labels related to single log line only. |
| 249 | + * @since 1.1.1 |
| 250 | + */ |
| 251 | + void unknown(final String line, final Labels structuredMetadata) { |
| 252 | + unknown().log(line, structuredMetadata); |
| 253 | + } |
| 254 | +} |
0 commit comments