Open History to see the last commit
1/**
2 * Log-level types and helpers used by Effect logging. The module defines all
3 * accepted log levels, the concrete emitted severities, the ordered level list,
4 * equality and ordering instances, threshold comparison helpers, and an effect
5 * for checking whether a level is enabled by the current logging settings.
6 *
7 * @since 2.0.0
8 */
9import type * as Effect from "./Effect.ts"
10import * as Equ from "./Equivalence.ts"
11import * as core from "./internal/core.ts"
12import * as effect from "./internal/effect.ts"
13import * as Ord from "./Order.ts"
14import * as References from "./References.ts"
15
16/**
17 * Represents every level used by Effect logging, including concrete message
18 * severities and the `All` and `None` sentinel levels.
19 *
20 * **When to use**
21 *
22 * Use to type values that may be either concrete log message severities or
23 * logging configuration sentinels.
24 *
25 * **Details**
26 *
27 * The levels are ordered from most severe to least severe:
28 * - `All` - Special level that allows all messages
29 * - `Fatal` - System is unusable, immediate attention required
30 * - `Error` - Error conditions that should be investigated
31 * - `Warn` - Warning conditions that may indicate problems
32 * - `Info` - Informational messages about normal operation
33 * - `Debug` - Debug information useful during development
34 * - `Trace` - Very detailed trace information
35 * - `None` - Special level that suppresses all messages
36 *
37 * **Example** (Using log levels)
38 *
39 * ```ts
40 * import { Effect } from "effect"
41 *
42 * // Using log levels with Effect logging
43 * const program = Effect.gen(function*() {
44 * yield* Effect.logFatal("System failure")
45 * yield* Effect.logError("Database error")
46 * yield* Effect.logWarning("High memory usage")
47 * yield* Effect.logInfo("User logged in")
48 * yield* Effect.logDebug("Processing request")
49 * yield* Effect.logTrace("Variable state")
50 * })
51 *
52 * // Type-safe log level variables
53 * const errorLevel = "Error" // LogLevel
54 * const debugLevel = "Debug" // LogLevel
55 * ```
56 *
57 * @category models
58 * @since 2.0.0
59 */
60export type LogLevel = "All" | "Fatal" | "Error" | "Warn" | "Info" | "Debug" | "Trace" | "None"
61
62/**
63 * Log levels that represent actual message severities, excluding the `All` and
64 * `None` sentinel levels.
65 *
66 * **When to use**
67 *
68 * Use when typing emitted log message severities, such as explicit log calls,
69 * current log level references, or error-report severity annotations, where
70 * `All` and `None` are not valid values.
71 *
72 * @see {@link LogLevel} for the wider log-level type that also accepts the
73 * `All` and `None` sentinel levels
74 * @see {@link values} for the runtime list of all accepted `LogLevel` values,
75 * including sentinels
76 *
77 * @category models
78 * @since 4.0.0
79 */
80export type Severity = "Fatal" | "Error" | "Warn" | "Info" | "Debug" | "Trace"
81
82/**
83 * Returns all `LogLevel` values in order from `All` through the concrete severities to
84 * `None`.
85 *
86 * **When to use**
87 *
88 * Use to enumerate or validate all accepted `LogLevel` string values, including
89 * the `All` and `None` sentinel levels.
90 *
91 * **Details**
92 *
93 * The array order matches the module severity order: `All`, concrete
94 * severities from `Fatal` to `Trace`, then `None`.
95 *
96 * **Gotchas**
97 *
98 * This list includes `All` and `None`, so it is not limited to concrete emitted
99 * severities.
100 *
101 * @see {@link Severity} for the concrete message severity type that excludes `All` and `None`
102 * @see {@link Order} for comparing these levels by severity order
103 *
104 * @category models
105 * @since 4.0.0
106 */
107export const values: ReadonlyArray<LogLevel> = ["All", "Fatal", "Error", "Warn", "Info", "Debug", "Trace", "None"]
108
109/**
110 * Order instance for `LogLevel` that defines the severity ordering.
111 *
112 * **When to use**
113 *
114 * Use to sort or compare log levels according to Effect's severity order.
115 *
116 * **Details**
117 *
118 * This order treats "All" as the least restrictive level and "None" as the most restrictive,
119 * with Fatal being the most severe actual log level.
120 *
121 * **Example** (Ordering log levels)
122 *
123 * ```ts
124 * import { LogLevel } from "effect"
125 *
126 * // Compare log levels using Order
127 * console.log(LogLevel.Order("Error", "Info")) // 1 (Error > Info)
128 * console.log(LogLevel.Order("Debug", "Error")) // -1 (Debug < Error)
129 * console.log(LogLevel.Order("Info", "Info")) // 0 (Info == Info)
130 * ```
131 *
132 * @category ordering
133 * @since 2.0.0
134 */
135export const Order: Ord.Order<LogLevel> = effect.LogLevelOrder
136
137/**
138 * Equivalence instance for log levels using strict equality (`===`).
139 *
140 * **When to use**
141 *
142 * Use to compare two `LogLevel` values when only the exact same level should
143 * match.
144 *
145 * **Details**
146 *
147 * Each log level string, including `All` and `None`, only matches itself.
148 *
149 * **Example** (Comparing log levels)
150 *
151 * ```ts
152 * import { LogLevel } from "effect"
153 *
154 * console.log(LogLevel.Equivalence("Error", "Error")) // true
155 * console.log(LogLevel.Equivalence("Error", "Info")) // false
156 * ```
157 *
158 * @see {@link Order} for severity ordering rather than exact level equality
159 * @see {@link isGreaterThanOrEqualTo} for minimum-threshold checks
160 *
161 * @category instances
162 * @since 4.0.0
163 */
164export const Equivalence: Equ.Equivalence<LogLevel> = Equ.strictEqual<LogLevel>()
165
166/**
167 * Returns the ordinal value of the log level.
168 *
169 * **When to use**
170 *
171 * Use to project a `LogLevel` into the numeric sort key used by
172 * `LogLevel.Order` when custom ordering code or an integration needs a number
173 * instead of an `Order` comparison.
174 *
175 * **Details**
176 *
177 * The mapping is `All` to `Number.MIN_SAFE_INTEGER`, `Trace` to `0`, `Debug` to
178 * `10000`, `Info` to `20000`, `Warn` to `30000`, `Error` to `40000`, `Fatal` to
179 * `50000`, and `None` to `Number.MAX_SAFE_INTEGER`.
180 *
181 * **Gotchas**
182 *
183 * These ordinals are internal sort keys; do not treat them as external severity
184 * numbers.
185 *
186 * @see {@link Order} for comparing log levels without exposing numeric keys
187 * @see {@link isGreaterThanOrEqualTo} for minimum-threshold filtering
188 *
189 * @category ordering
190 * @since 4.0.0
191 */
192export const getOrdinal = (self: LogLevel): number => effect.logLevelToOrder(self)
193
194/**
195 * Determines if the first log level is more severe than the second.
196 *
197 * **When to use**
198 *
199 * Use to check whether one log level is strictly more severe than another.
200 *
201 * **Details**
202 *
203 * Returns `true` if `self` represents a more severe level than `that`.
204 *
205 * **Example** (Checking higher severity)
206 *
207 * ```ts
208 * import { LogLevel } from "effect"
209 *
210 * // Check if Error is more severe than Info
211 * console.log(LogLevel.isGreaterThan("Error", "Info")) // true
212 * console.log(LogLevel.isGreaterThan("Debug", "Error")) // false
213 *
214 * // Use with filtering
215 * const isFatal = LogLevel.isGreaterThan("Fatal", "Warn")
216 * const isError = LogLevel.isGreaterThan("Error", "Warn")
217 * const isDebug = LogLevel.isGreaterThan("Debug", "Warn")
218 * console.log(isFatal) // true
219 * console.log(isError) // true
220 * console.log(isDebug) // false
221 *
222 * // Curried usage
223 * const isMoreSevereThanInfo = LogLevel.isGreaterThan("Info")
224 * console.log(isMoreSevereThanInfo("Error")) // true
225 * console.log(isMoreSevereThanInfo("Debug")) // false
226 * ```
227 *
228 * @category ordering
229 * @since 4.0.0
230 */
231export const isGreaterThan: {
232 (that: LogLevel): (self: LogLevel) => boolean
233 (self: LogLevel, that: LogLevel): boolean
234} = effect.isLogLevelGreaterThan
235
236/**
237 * Determines if the first log level is more severe than or equal to the second.
238 *
239 * **When to use**
240 *
241 * Use to implement minimum log-level filtering by checking whether a message
242 * level meets a threshold.
243 *
244 * **Details**
245 *
246 * Returns `true` if `self` represents a level that is more severe than or equal to `that`.
247 *
248 * **Example** (Filtering by minimum log level)
249 *
250 * ```ts
251 * import { Logger, LogLevel } from "effect"
252 *
253 * // Check if level meets minimum threshold
254 * console.log(LogLevel.isGreaterThanOrEqualTo("Error", "Error")) // true
255 * console.log(LogLevel.isGreaterThanOrEqualTo("Error", "Info")) // true
256 * console.log(LogLevel.isGreaterThanOrEqualTo("Debug", "Info")) // false
257 *
258 * // Create a logger that only logs Info and above
259 * const infoLogger = Logger.make((options) => {
260 * if (LogLevel.isGreaterThanOrEqualTo(options.logLevel, "Info")) {
261 * console.log(`[${options.logLevel}] ${options.message}`)
262 * }
263 * })
264 *
265 * // Production logger - only Error and Fatal
266 * const productionLogger = Logger.make((options) => {
267 * if (LogLevel.isGreaterThanOrEqualTo(options.logLevel, "Error")) {
268 * console.error(
269 * `${options.date.toISOString()} [${options.logLevel}] ${options.message}`
270 * )
271 * }
272 * })
273 *
274 * // Curried usage for filtering
275 * const isInfoOrAbove = LogLevel.isGreaterThanOrEqualTo("Info")
276 * const shouldLog = isInfoOrAbove("Error") // true
277 * ```
278 *
279 * @category ordering
280 * @since 4.0.0
281 */
282export const isGreaterThanOrEqualTo: {
283 (that: LogLevel): (self: LogLevel) => boolean
284 (self: LogLevel, that: LogLevel): boolean
285} = Ord.isGreaterThanOrEqualTo(Order)
286
287/**
288 * Determines if the first log level is less severe than the second.
289 *
290 * **When to use**
291 *
292 * Use to check whether one log level is strictly less severe than another.
293 *
294 * **Details**
295 *
296 * Returns `true` if `self` represents a less severe level than `that`.
297 *
298 * **Example** (Checking lower severity)
299 *
300 * ```ts
301 * import { LogLevel } from "effect"
302 *
303 * // Check if Debug is less severe than Info
304 * console.log(LogLevel.isLessThan("Debug", "Info")) // true
305 * console.log(LogLevel.isLessThan("Error", "Info")) // false
306 *
307 * // Filter out verbose logs
308 * const isFatalVerbose = LogLevel.isLessThan("Fatal", "Info")
309 * const isErrorVerbose = LogLevel.isLessThan("Error", "Info")
310 * const isTraceVerbose = LogLevel.isLessThan("Trace", "Info")
311 * console.log(isFatalVerbose) // false (Fatal is not verbose)
312 * console.log(isErrorVerbose) // false (Error is not verbose)
313 * console.log(isTraceVerbose) // true (Trace is verbose)
314 *
315 * // Curried usage
316 * const isLessSevereThanError = LogLevel.isLessThan("Error")
317 * console.log(isLessSevereThanError("Info")) // true
318 * console.log(isLessSevereThanError("Fatal")) // false
319 * ```
320 *
321 * @category ordering
322 * @since 4.0.0
323 */
324export const isLessThan: {
325 (that: LogLevel): (self: LogLevel) => boolean
326 (self: LogLevel, that: LogLevel): boolean
327} = Ord.isLessThan(Order)
328
329/**
330 * Determines if the first log level is less severe than or equal to the second.
331 *
332 * **When to use**
333 *
334 * Use to implement maximum log-level filtering by checking whether a level is
335 * at or below a threshold.
336 *
337 * **Details**
338 *
339 * Returns `true` if `self` represents a level that is less severe than or equal to `that`.
340 *
341 * **Example** (Filtering by maximum log level)
342 *
343 * ```ts
344 * import { Logger, LogLevel } from "effect"
345 *
346 * // Check if level is at or below threshold
347 * console.log(LogLevel.isLessThanOrEqualTo("Info", "Info")) // true
348 * console.log(LogLevel.isLessThanOrEqualTo("Debug", "Info")) // true
349 * console.log(LogLevel.isLessThanOrEqualTo("Error", "Info")) // false
350 *
351 * // Create a logger that suppresses verbose logs
352 * const quietLogger = Logger.make((options) => {
353 * if (LogLevel.isLessThanOrEqualTo(options.logLevel, "Info")) {
354 * console.log(`[${options.logLevel}] ${options.message}`)
355 * }
356 * })
357 *
358 * // Development logger - suppress trace logs
359 * const devLogger = Logger.make((options) => {
360 * if (LogLevel.isLessThanOrEqualTo(options.logLevel, "Debug")) {
361 * console.log(`[${options.logLevel}] ${options.message}`)
362 * }
363 * })
364 *
365 * // Curried usage for filtering
366 * const isInfoOrBelow = LogLevel.isLessThanOrEqualTo("Info")
367 * const shouldLog = isInfoOrBelow("Debug") // true
368 * ```
369 *
370 * @category ordering
371 * @since 4.0.0
372 */
373export const isLessThanOrEqualTo: {
374 (that: LogLevel): (self: LogLevel) => boolean
375 (self: LogLevel, that: LogLevel): boolean
376} = Ord.isLessThanOrEqualTo(Order)
377
378/**
379 * Checks whether a given log level is enabled for the current fiber.
380 *
381 * **When to use**
382 *
383 * Use to check whether a log level would be emitted under the current fiber's
384 * minimum log level.
385 *
386 * **Details**
387 *
388 * A log level is enabled when it is greater than or equal to
389 * `References.MinimumLogLevel`.
390 *
391 * **Example** (Checking current fiber log level)
392 *
393 * ```ts
394 * import { Effect, LogLevel, References } from "effect"
395 *
396 * const program = Effect.gen(function*() {
397 * const debugEnabled = yield* LogLevel.isEnabled("Debug")
398 * const errorEnabled = yield* LogLevel.isEnabled("Error")
399 *
400 * console.log({ debugEnabled, errorEnabled })
401 * })
402 *
403 * const warnOnly = program.pipe(
404 * Effect.provideService(References.MinimumLogLevel, "Warn")
405 * )
406 * ```
407 *
408 * @category filtering
409 * @since 4.0.0
410 */
411export const isEnabled = (self: LogLevel): Effect.Effect<boolean> =>
412 core.withFiber((fiber) => effect.succeed(!isGreaterThan(fiber.getRef(References.MinimumLogLevel), self)))