Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add FirebaseLogger #13238

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 89 additions & 0 deletions FirebaseSharedSwift/Sources/FirebaseInternalLog.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
// Copyright 2024 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

import Foundation
import OSLog

protocol FirebaseInternalLog {
func notice(_ message: String)

func info(_ message: String)

func debug(_ message: String)

func warning(_ message: String)

func error(_ message: String)

func fault(_ message: String)
}

extension OSLog: FirebaseInternalLog {
func notice(_ message: String) {
os_log("%s", log: self, type: .default, message)
}

func info(_ message: String) {
os_log("%s", log: self, type: .info, message)
}

func debug(_ message: String) {
os_log("%s", log: self, type: .debug, message)
}

func warning(_ message: String) {
os_log("%s", log: self, type: .default, message)
}

func error(_ message: String) {
os_log("%s", log: self, type: .error, message)
}

func fault(_ message: String) {
os_log("%s", log: self, type: .fault, message)
}
}

@available(macOS 11.0, iOS 14.0, watchOS 7.0, tvOS 14.0, *)
class FirebaseInternalLogger: FirebaseInternalLog {
private let logger: Logger

required init(subsystem: String, category: String) {
logger = Logger(subsystem: subsystem, category: category)
}

func notice(_ message: String) {
logger.notice("\(message)")
}

func info(_ message: String) {
logger.info("\(message)")
}

func debug(_ message: String) {
logger.debug("\(message)")
}

func warning(_ message: String) {
logger.warning("\(message)")
}

func error(_ message: String) {
logger.error("\(message)")
}

func fault(_ message: String) {
logger.fault("\(message)")
}
}
66 changes: 66 additions & 0 deletions FirebaseSharedSwift/Sources/FirebaseLogger.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// Copyright 2024 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

import Foundation
import OSLog

public struct FirebaseProduct {
var name: String

public init(name: String) {
self.name = name
}
}

public class FirebaseLogger {
let subsystem: String = "com.google.firebase"

let firebaseProduct: FirebaseProduct

private lazy var logger: FirebaseInternalLog = {
if #available(macOS 11.0, iOS 14.0, watchOS 7.0, tvOS 14.0, *) {
return FirebaseInternalLogger(subsystem: subsystem, category: firebaseProduct.name)
} else {
return OSLog(subsystem: subsystem, category: firebaseProduct.name)
}
}()

public init(firebaseProduct: FirebaseProduct) {
self.firebaseProduct = firebaseProduct
}

public func notice(_ message: String) {
logger.notice(message)
}

public func info(_ message: String) {
logger.info(message)
}

public func debug(_ message: String) {
logger.debug(message)
}

public func warning(_ message: String) {
logger.warning(message)
}

public func error(_ message: String) {
logger.error(message)
}

public func fault(_ message: String) {
logger.fault(message)
}
}
34 changes: 34 additions & 0 deletions FirebaseSharedSwift/Tests/FirebaseLoggerTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Copyright 2024 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

import FirebaseSharedSwift
import Foundation

// MARK: - Test Suite

import XCTest

extension FirebaseProduct {
static let test = FirebaseProduct(name: "test")
}

extension FirebaseLogger {
static let testLogger = FirebaseLogger(firebaseProduct: FirebaseProduct.test)
}

class TestFirebaseLogger: XCTestCase {
func testLoggerUsage() {
FirebaseLogger.testLogger.debug("Set up successfully!")
}
}
Loading