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

setcustomlogger implementation on iOS #1026

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public class PrebidAdMobMediationBaseAdapter: NSObject, GADMediationAdapter {

// Added for tests
static func latestTestedGMAVersion() -> GADVersionNumber {
return GADVersionNumber(majorVersion: 11, minorVersion: 6, patchVersion: 0)
return GADVersionNumber(majorVersion: 11, minorVersion: 7, patchVersion: 0)
}

required public override init() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public class GAMUtils: NSObject {

// Added for tests
static func latestTestedGMAVersion() -> GADVersionNumber {
return GADVersionNumber(majorVersion: 11, minorVersion: 6, patchVersion: 0)
return GADVersionNumber(majorVersion: 11, minorVersion: 7, patchVersion: 0)
}

// MARK: Private Methods
Expand Down
4 changes: 4 additions & 0 deletions PrebidMobile.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -771,6 +771,7 @@
9720387C2358771600F8025A /* NativeRequestTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9720387B2358771600F8025A /* NativeRequestTests.swift */; };
9743CB84235F1CDB002E2CAA /* NativeAssetTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9743CB83235F1CDB002E2CAA /* NativeAssetTests.swift */; };
9743CB86235F264B002E2CAA /* NativeEventTrackerTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9743CB85235F264B002E2CAA /* NativeEventTrackerTests.swift */; };
F91B60A92C52A73500DA9796 /* SDKConsoleLogger.swift in Sources */ = {isa = PBXBuildFile; fileRef = F91B60A82C52A73500DA9796 /* SDKConsoleLogger.swift */; };
FA5AD5E42271FA4100C8F274 /* ConstantsTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = FA5AD5E32271FA4100C8F274 /* ConstantsTest.swift */; };
FA9D7F2722E8A83D006FCBEF /* AdViewUtilsTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = FA9D7F2622E8A83D006FCBEF /* AdViewUtilsTests.swift */; };
FAA29904242D1C27002ACBF2 /* TargetingObjCTests.m in Sources */ = {isa = PBXBuildFile; fileRef = FAA29903242D1C27002ACBF2 /* TargetingObjCTests.m */; };
Expand Down Expand Up @@ -1654,6 +1655,7 @@
97826AA621FB4F1B001E2C05 /* Constants.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Constants.swift; sourceTree = "<group>"; };
9791778F2201AF4700E624CE /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
979177962201AF5F00E624CE /* DispatcherTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DispatcherTests.swift; sourceTree = "<group>"; };
F91B60A82C52A73500DA9796 /* SDKConsoleLogger.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SDKConsoleLogger.swift; sourceTree = "<group>"; };
FA4A88432497A99D00FDCBB6 /* Swizzling.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Swizzling.swift; sourceTree = "<group>"; };
FA5AD5E32271FA4100C8F274 /* ConstantsTest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ConstantsTest.swift; sourceTree = "<group>"; };
FA85F9B4264946FC00B8BE72 /* TestUtils.xcodeproj */ = {isa = PBXFileReference; lastKnownFileType = "wrapper.pb-project"; name = TestUtils.xcodeproj; path = ../tools/TestUtils/TestUtils.xcodeproj; sourceTree = "<group>"; };
Expand Down Expand Up @@ -1878,6 +1880,7 @@
FAEE4CE2262DC2B200AD9966 /* Log.swift */,
531CF21827E8FC1B005E5ABE /* LogLevel.swift */,
53F096C927E917D40058C94C /* Log+Extensions.h */,
F91B60A82C52A73500DA9796 /* SDKConsoleLogger.swift */,
);
path = Logging;
sourceTree = "<group>";
Expand Down Expand Up @@ -4205,6 +4208,7 @@
5BC3792F271F1D0000444D5E /* PBMDeviceAccessManager.m in Sources */,
5BC37A6E271F1D0000444D5E /* PBMORTBBidResponse.m in Sources */,
5BC37ACE271F1D0100444D5E /* PBMGeoLocationParameterBuilder.m in Sources */,
F91B60A92C52A73500DA9796 /* SDKConsoleLogger.swift in Sources */,
5BC37A8C271F1D0000444D5E /* BaseInterstitialAdUnitProtocol.swift in Sources */,
5BC3797A271F1D0000444D5E /* PBMVastRequester.m in Sources */,
53322AA3282D45EE0049229D /* PrebidServerEventTracker.swift in Sources */,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import Foundation
class PrebidGAMVersionChecker {

var latestTestedGMAVersion: (Int, Int, Int) {
(11, 6, 0)
(11, 7, 0)
}

var currentGMAVersion: (Int, Int, Int)?
Expand Down
34 changes: 33 additions & 1 deletion PrebidMobile/Logging/Log.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import Foundation
public class Log: NSObject {

// MARK: - Public properties

public static var logger: PrebidLogger? = SDKConsoleLogger()
public static var dateFormat = "yyyy-MM-dd hh:mm:ssSSS"
public static var dateFormatter: DateFormatter {
let formatter = DateFormatter()
Expand All @@ -29,35 +29,46 @@ public class Log: NSObject {
return formatter
}

public static func setCustomLogger(customLogger: PrebidLogger?) {
logger = customLogger
}

public static var logLevel: LogLevel = .debug
public static var logToFile = false

public static func error(_ object: Any, filename: String = #file, line: Int = #line, function: String = #function) {
log(object, logLevel: .error, filename: filename, line: line, function: function)
jsligh marked this conversation as resolved.
Show resolved Hide resolved
logger?.error(object, filename: filename, line: line, function: function)
}

public static func info(_ object: Any, filename: String = #file, line: Int = #line, function: String = #function) {
log(object, logLevel: .info, filename: filename, line: line, function: function)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now, you should remove system fogging from the Log class. Otherwise, the same messages will be logged twice.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They shouldn't because the SDKConsoleLogger's log method doesn't post to the system log.

logger?.info(object, filename: filename, line: line, function: function)
}

public static func debug(_ object: Any, filename: String = #file, line: Int = #line, function: String = #function) {
log(object, logLevel: .debug, filename: filename, line: line, function: function)
logger?.debug(object, filename: filename, line: line, function: function)
}

public static func verbose(_ object: Any, filename: String = #file, line: Int = #line, function: String = #function) {
log(object, logLevel: .verbose, filename: filename, line: line, function: function)
logger?.verbose(object, filename: filename, line: line, function: function)
}

public static func warn(_ object: Any, filename: String = #file, line: Int = #line, function: String = #function) {
log(object, logLevel: .warn, filename: filename, line: line, function: function)
logger?.warn(object, filename: filename, line: line, function: function)
}

public static func severe(_ object: Any, filename: String = #file, line: Int = #line, function: String = #function) {
log(object, logLevel: .severe, filename: filename, line: line, function: function)
logger?.severe(object, filename: filename, line: line, function: function)
}

public static func whereAmI(filename: String = #file, line: Int = #line, function: String = #function) {
log("", logLevel: .info, filename: filename, line: line, function: function)
logger?.whereAmI(filename: filename, line: line, function: function)
}

static func log(_ object: Any, logLevel: LogLevel, filename: String, line: Int, function: String) {
Expand All @@ -66,6 +77,7 @@ public class Log: NSObject {
print(finalMessage)
serialWriteToLog(finalMessage)
}
logger?.log(object, logLevel: logLevel, filename: filename, line: line, function: function)
}

public static func serialWriteToLog(_ message: String) {
Expand Down Expand Up @@ -186,3 +198,23 @@ func print(_ object: Any) {
Swift.print(object)
#endif
}

public protocol PrebidLogger {

func error(_ object: Any, filename: String, line: Int, function: String)

func info(_ object: Any, filename: String, line: Int, function: String)

func debug(_ object: Any, filename: String, line: Int, function: String)

func verbose(_ object: Any, filename: String, line: Int, function: String)

func warn(_ object: Any, filename: String, line: Int, function: String)

func severe(_ object: Any, filename: String, line: Int, function: String)

func whereAmI(filename: String, line: Int, function: String)

func log(_ object: Any, logLevel: LogLevel, filename: String, line: Int, function: String)

}
45 changes: 45 additions & 0 deletions PrebidMobile/Logging/SDKConsoleLogger.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
//
// SDKConsoleLogger.swift
// PrebidMobile
//
// Created by Jono Sligh on 7/25/24.
// Copyright © 2024 AppNexus. All rights reserved.
//

import Foundation

@objc(PBMSDKConsoleLogger) @objcMembers
public class SDKConsoleLogger: NSObject, PrebidLogger {

public func error(_ object: Any, filename: String, line: Int, function: String) {
log(object, logLevel: .error, filename: filename, line: line, function: function)
}

public func info(_ object: Any, filename: String, line: Int, function: String) {
log(object, logLevel: .info, filename: filename, line: line, function: function)
}

public func debug(_ object: Any, filename: String, line: Int, function: String) {
log(object, logLevel: .debug, filename: filename, line: line, function: function)
}

public func verbose(_ object: Any, filename: String, line: Int, function: String) {
log(object, logLevel: .verbose, filename: filename, line: line, function: function)
}

public func warn(_ object: Any, filename: String, line: Int, function: String) {
log(object, logLevel: .warn, filename: filename, line: line, function: function)
}

public func severe(_ object: Any, filename: String, line: Int, function: String) {
log(object, logLevel: .severe, filename: filename, line: line, function: function)
}

public func whereAmI(filename: String, line: Int, function: String) {

}

public func log(_ object: Any, logLevel: LogLevel, filename: String, line: Int, function: String) {

}
}
77 changes: 77 additions & 0 deletions PrebidMobileTests/RenderingTests/Tests/LogTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,35 @@ class LogTest: XCTestCase {
checkLogAndClean(level: .severe)
}

func testAllKindsWithCustom() {
// Test: default params

logToFile = .init()

Log.setCustomLogger(customLogger: CustomTestLogger())

Log.error(message)
let log = Log.getLogFileAsString() ?? ""
XCTAssert(log.contains("TESTLOG"))
checkLogAndClean(level: .error)

Log.info(message)
checkLogAndClean(level: .info)

Log.debug(message)
checkLogAndClean(level: .debug)

Log.verbose(message)
checkLogAndClean(level: .verbose)

Log.warn(message)
checkLogAndClean(level: .warn)

Log.severe(message)
checkLogAndClean(level: .severe)
Log.setCustomLogger(customLogger: nil)
}

func testWhereAmI() {
logToFile = .init()

Expand All @@ -116,6 +145,17 @@ class LogTest: XCTestCase {
XCTAssertTrue(log.contains(LogLevel.info.stringValue))
}

func testWhereAmICustom() {
logToFile = .init()
Log.setCustomLogger(customLogger: CustomTestLogger())

Log.whereAmI()

let log = Log.getLogFileAsString() ?? ""
XCTAssertTrue(log.contains("WHEREAMI"))
Log.setCustomLogger(customLogger: nil)
}

func testLogLevel() {
logToFile = .init()

Expand Down Expand Up @@ -151,4 +191,41 @@ class LogTest: XCTestCase {
logToFile = nil
logToFile = .init()
}

class CustomTestLogger: PrebidLogger {
func error(_ object: Any, filename: String, line: Int, function: String) {
log(object, logLevel: .error, filename: filename, line: line, function: function)
}

func info(_ object: Any, filename: String, line: Int, function: String) {
log(object, logLevel: .info, filename: filename, line: line, function: function)
}

func debug(_ object: Any, filename: String, line: Int, function: String) {
log(object, logLevel: .debug, filename: filename, line: line, function: function)
}

func verbose(_ object: Any, filename: String, line: Int, function: String) {
log(object, logLevel: .verbose, filename: filename, line: line, function: function)
}

func warn(_ object: Any, filename: String, line: Int, function: String) {
log(object, logLevel: .warn, filename: filename, line: line, function: function)
}

func severe(_ object: Any, filename: String, line: Int, function: String) {
log(object, logLevel: .severe, filename: filename, line: line, function: function)
}

func whereAmI(filename: String, line: Int, function: String) {
log("WHEREAMI", logLevel: .info, filename: filename, line: line, function: function)
}

func log(_ object: Any, logLevel: PrebidMobile.LogLevel, filename: String, line: Int, function: String) {
let finalMessage = "\(logLevel.stringValue) \(object): TESTLOG"
print(finalMessage)
Log.serialWriteToLog(finalMessage)
}

}
}