Skip to content

Commit

Permalink
Set log level from LOG_LEVEL env var
Browse files Browse the repository at this point in the history
  • Loading branch information
adam-fowler committed Mar 30, 2021
1 parent 66af6d7 commit 21e7c30
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Sources/Hummingbird/Application.swift
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public final class HBApplication: HBExtensible {
var logger = Logger(label: "HummingBird")
logger.logLevel = configuration.logLevel
self.logger = logger

// create eventLoopGroup
self.eventLoopGroupProvider = eventLoopGroupProvider
switch eventLoopGroupProvider {
Expand Down
13 changes: 11 additions & 2 deletions Sources/Hummingbird/Configuration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,10 @@ extension HBApplication {
tcpNoDelay: Bool = false,
enableHttpPipelining: Bool = true,
threadPoolSize: Int = 2,
logLevel: Logger.Level = .info
logLevel: Logger.Level? = nil
) {
let env = HBEnvironment()

self.address = address
self.serverName = serverName
self.maxUploadSize = maxUploadSize
Expand All @@ -55,7 +57,14 @@ extension HBApplication {
self.enableHttpPipelining = enableHttpPipelining

self.threadPoolSize = threadPoolSize
self.logLevel = logLevel

if let logLevel = logLevel {
self.logLevel = logLevel
} else if let logLevel = env.get("LOG_LEVEL") {
self.logLevel = Logger.Level(rawValue: logLevel) ?? .info
} else {
self.logLevel = .info
}
}

/// return HTTP server configuration
Expand Down
6 changes: 6 additions & 0 deletions Tests/HummingbirdTests/EnvironmentTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,10 @@ final class EnvironmentTests: XCTestCase {
env.set("TEST_VAR", value: "testSet")
XCTAssertEqual(env.get("TEST_VAR"), "testSet")
}

func testLogLevel() {
setenv("LOG_LEVEL", "trace", 1)
let app = HBApplication()
XCTAssertEqual(app.logger.logLevel, .trace)
}
}

0 comments on commit 21e7c30

Please sign in to comment.