mirror of
https://github.com/apple/swift-log
synced 2026-11-25 03:57:48 +00:00
Multiple proposal improvements to the API (#41)
* Move `Logging.make` to a `Logger` initializer. * Remove all factory initializers in example and test code.
This commit is contained in:
1 parent
03174f3260
commit
2474d61a80
19 files changed
+336
-376
No files matched your search
@@ -9,13 +9,13 @@ import Logging
|
||||
enum ConfigExample {
|
||||
static func main() {
|
||||
// boostrap with our config based sample implementations
|
||||
let logging = ConfigLogging()
|
||||
Logging.bootstrap(logging.make)
|
||||
let config = Config(defaultLogLevel: .info)
|
||||
Logging.bootstrap({ ConfigLogHandler(label: $0, config: config) })
|
||||
// run the example
|
||||
let logger = Logging.make("com.example.TestApp")
|
||||
let logger = Logger(label: "com.example.TestApp")
|
||||
logger.trace("hello world?")
|
||||
// changes the config in runtime, real implemnetations will use config files instead of in-memory config
|
||||
logging.config.set(key: "com.example.TestApp", value: .trace)
|
||||
config.set(key: "com.example.TestApp", value: .trace)
|
||||
logger.trace("hello world!")
|
||||
}
|
||||
}
|
||||
@@ -12,8 +12,7 @@ import Logging
|
||||
enum ContextBasedSystem {
|
||||
static func main() {
|
||||
// boostrap with our sample implementation
|
||||
let logging = SimpleLogging()
|
||||
Logging.bootstrap(logging.make)
|
||||
Logging.bootstrap(SimpleLogHandler.init)
|
||||
// run the example
|
||||
for i in 1 ... 2 {
|
||||
print("---------------------------- processing request #\(i) ----------------------------")
|
||||
@@ -23,14 +22,14 @@ enum ContextBasedSystem {
|
||||
}
|
||||
|
||||
struct Context {
|
||||
var logger = Logging.make("ContextLogger")
|
||||
var logger = Logger(label: "ContextLogger")
|
||||
|
||||
init(metadata: Logging.Metadata = [:]) {
|
||||
init(metadata: Logger.Metadata = [:]) {
|
||||
metadata.forEach { self[$0.0] = $0.1 }
|
||||
}
|
||||
|
||||
// since logger is a value type, we can reuse our copy to manage the metadata
|
||||
subscript(metadataKey: String) -> Logging.Metadata.Value? {
|
||||
subscript(metadataKey: String) -> Logger.Metadata.Value? {
|
||||
get { return self.logger[metadataKey: metadataKey] }
|
||||
set { self.logger[metadataKey: metadataKey] = newValue }
|
||||
}
|
||||
|
||||
@@ -7,7 +7,7 @@ import Foundation
|
||||
import Logging
|
||||
|
||||
// since this example mutates the global logger, we need to lock around it
|
||||
var _logger = Logging.make("GlobalLogger")
|
||||
var _logger = Logger(label: "GlobalLogger")
|
||||
var lock = NSLock()
|
||||
var logger: Logger {
|
||||
get {
|
||||
@@ -26,8 +26,7 @@ var logger: Logger {
|
||||
enum GlobalLoggerBasedSystem {
|
||||
static func main() {
|
||||
// boostrap with our sample implementation
|
||||
let logging = SimpleLogging()
|
||||
Logging.bootstrap(logging.make)
|
||||
Logging.bootstrap(SimpleLogHandler.init)
|
||||
// run the example
|
||||
for i in 1 ... 2 {
|
||||
print("---------------------------- processing request #\(i) ----------------------------")
|
||||
|
||||
@@ -10,8 +10,7 @@ import Logging
|
||||
enum LocalLoggerBasedSystem {
|
||||
static func main() {
|
||||
// boostrap with our sample implementation
|
||||
let logging = SimpleLogging()
|
||||
Logging.bootstrap(logging.make)
|
||||
Logging.bootstrap(SimpleLogHandler.init)
|
||||
// run the example
|
||||
for i in 1 ... 2 {
|
||||
print("---------------------------- processing request #\(i) ----------------------------")
|
||||
@@ -20,7 +19,7 @@ enum LocalLoggerBasedSystem {
|
||||
}
|
||||
|
||||
class Foo {
|
||||
private var logger = Logging.make("LocalLoggerBasedSystem::Foo")
|
||||
private var logger = Logger(label: "LocalLoggerBasedSystem::Foo")
|
||||
|
||||
func doSomething(requestNumnber: Int) {
|
||||
if 0 == requestNumnber % 2 {
|
||||
@@ -42,7 +41,7 @@ enum LocalLoggerBasedSystem {
|
||||
}
|
||||
|
||||
class Bar {
|
||||
private var logger = Logging.make("LocalLoggerBasedSystem::Bar")
|
||||
private var logger = Logger(label: "LocalLoggerBasedSystem::Bar")
|
||||
|
||||
func doSomething(requestNumnber: Int) {
|
||||
if 0 == requestNumnber % 2 {
|
||||
@@ -58,7 +57,7 @@ enum LocalLoggerBasedSystem {
|
||||
}
|
||||
|
||||
class Baz {
|
||||
private var logger = Logging.make("LocalLoggerBasedSystem::Baz")
|
||||
private var logger = Logger(label: "LocalLoggerBasedSystem::Baz")
|
||||
private let queue = DispatchQueue(label: "LocalLoggerBasedSystem::Baz")
|
||||
|
||||
func doSomething(requestNumnber: Int) {
|
||||
|
||||
@@ -9,10 +9,9 @@ import Logging
|
||||
enum MultiplexExample {
|
||||
static func main() {
|
||||
// boostrap with two of our sample implementations
|
||||
let logging = MultiplexLogging([SimpleLogging().make, FileLogging().make])
|
||||
Logging.bootstrap(logging.make)
|
||||
Logging.bootstrap({ MultiplexLogHandler([SimpleLogHandler(label: $0), FileLogHandler(label: $0)]) })
|
||||
// run the example
|
||||
let logger = Logging.make("com.example.TestApp")
|
||||
let logger = Logger(label: "com.example.TestApp")
|
||||
logger.info("hello world!")
|
||||
}
|
||||
}
|
||||
@@ -9,7 +9,7 @@ import Logging
|
||||
// this library does not know anything about the systewm that used it, so cannot make
|
||||
// assumptions about them
|
||||
class RandomLibrary {
|
||||
private let logger = Logging.make("RandomLibrary")
|
||||
private let logger = Logger(label: "RandomLibrary")
|
||||
private let queue = DispatchQueue(label: "RandomLibrary")
|
||||
|
||||
public init() {}
|
||||
|
||||
Reference in new issue
Block a user