forked from apple/swift-log
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLogEvent.swift
More file actions
116 lines (104 loc) · 4.36 KB
/
LogEvent.swift
File metadata and controls
116 lines (104 loc) · 4.36 KB
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
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift Logging API open source project
//
// Copyright (c) 2018-2019 Apple Inc. and the Swift Logging API project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of Swift Logging API project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
/// A log event that contains all the information about a single log statement.
///
/// `LogEvent` is passed to a ``LogHandler`` via ``LogHandler/log(event:)`` and carries every piece of data emitted
/// at the log call site. Handlers may inspect, modify, or forward any of the event's properties.
///
/// All properties are mutable so that handler wrappers (for example ``MultiplexLogHandler``) can rewrite fields
/// such as metadata or source before forwarding to downstream handlers.
public struct LogEvent: Sendable {
/// The log level of this event.
public var level: Logger.Level
/// The message of this event.
public var message: Logger.Message
/// The error associated with this event, if any.
public var error: (any Error)?
/// The metadata associated with this event, if any.
public var metadata: Logger.Metadata? {
get { self._metadata }
set { self._metadata = newValue }
}
private var _metadata: Logger.Metadata?
/// The source where this log event originated, for example the logging module.
///
/// When no explicit source was provided at the call site, this is derived lazily from ``file``
/// every time this property is accessed. Handlers that never read this property pay no
/// allocation cost for source computation.
public var source: String {
@inlinable get { self._source ?? Logger.currentModule(fileID: self.file) }
@inlinable set { self._source = newValue }
}
@usableFromInline
internal var _source: String?
/// The file this log event originates from.
public var file: String
/// The function this log event originates from.
public var function: String
/// The line this log event originates from.
public var line: UInt
/// Creates a new log event.
///
/// - Parameters:
/// - level: The log level of this event.
/// - message: The message of this event.
/// - metadata: The metadata associated with this event, if any.
/// - source: The source where this log event originated. When `nil`, ``source`` is derived lazily
/// from ``file`` on first access so handlers that never read it pay no allocation cost.
/// - file: The file this log event originates from.
/// - function: The function this log event originates from.
/// - line: The line this log event originates from.
public init(
level: Logger.Level,
message: Logger.Message,
metadata: Logger.Metadata?,
source: String?,
file: String,
function: String,
line: UInt
) {
self.init(level: level, message: message, error: nil, metadata: metadata, source: source, file: file, function: function, line: line)
}
/// Creates a new log event.
///
/// - Parameters:
/// - level: The log level of this event.
/// - message: The message of this event.
/// - error: The error associated with this event, if any.
/// - metadata: The metadata associated with this event, if any.
/// - source: The source where this log event originated. When `nil`, ``source`` is derived lazily
/// from ``file`` on first access so handlers that never read it pay no allocation cost.
/// - file: The file this log event originates from.
/// - function: The function this log event originates from.
/// - line: The line this log event originates from.
public init(
level: Logger.Level,
message: Logger.Message,
error: (any Error)?,
metadata: Logger.Metadata?,
source: String?,
file: String,
function: String,
line: UInt
) {
self.level = level
self.message = message
self.error = error
self._metadata = metadata
self._source = source
self.file = file
self.function = function
self.line = line
}
}