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

Log enhancements to allow for instances #180

Open
wants to merge 7 commits into
base: master
Choose a base branch
from

Conversation

jmagnini
Copy link

@jmagnini jmagnini commented May 26, 2021

Allow Log to be instanced, or referenced statically. Also send log messages to OSLog when available. Additionally, Trace level messages can be used for signposts in Instruments.

See: #173

2021-05-26_15-16-41

2021-05-27_14-05-20

@jmagnini jmagnini self-assigned this May 26, 2021
@jmagnini jmagnini linked an issue May 26, 2021 that may be closed by this pull request
/// Subsystem of the OSLog message when running on iOS 14 or later.
open var subsystem: String {
let bundleName = Bundle.main.object(forInfoDictionaryKey: "CFBundleDisplayName") as? String
return (bundleName ?? "com.rightpoint.swiftilities") + ".log"
Copy link
Author

@jmagnini jmagnini May 26, 2021

Choose a reason for hiding this comment

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

subsystem
A string that identifies the app subsystem in which to record messages. Specify this string using reverse DNS notation — for example, com.yourcompany.subsystem_name. The system uses this information to categorize and filter related log messages, and group related logging settings.

Given the definition from Apple. this seems like it should default to the bundle id of the parent application.

@jmagnini
Copy link
Author

looks like CI is using Xcode 12

@@ -62,7 +93,15 @@ open class Log {
/// your log statements! Use log levels appropriately
/// to keep private data out of logs that are sent over
/// the Internet.
public static var handler: ((Level, String) -> Void)?
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe this should be kept around and marked with a deprecated attribute, and shimmed with overridden get {} set {} to the new globalHandler

@available(*, deprecated, renamed: "globalHandler")

Copy link
Author

Choose a reason for hiding this comment

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

it's being kept as an Instance level handler. This allow for more selective handling if there are a bunch of different Log instances floating around.

Comment on lines +188 to +191
public static var handler: InstanceLogHandler? {
get { instance.handler }
set { instance.handler = newValue }
}
Copy link
Contributor

@chrisballinger chrisballinger May 26, 2021

Choose a reason for hiding this comment

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

Oh! I see the compatibility shim is down here. Might be good to mark some of these as deprecated/renamed

Copy link
Author

Choose a reason for hiding this comment

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

do we want them deprecated? For apps where a single Log instance is sufficient, I can still see some value keeping these shorthand versions around

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah perhaps not deprecated, but specifically the change related to the renaming of handler and globalHandler maybe. Also this one in particular is a bit confusing because it's an InstanceLogHandler but applied globally

Copy link
Author

@jmagnini jmagnini May 26, 2021

Choose a reason for hiding this comment

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

it's applied to the shared instance, but not necessarily globally. I agree the name is a little confusing though 🤔
Mainly, this is here for backwards compatibility.

Comment on lines 56 to 61
public static private(set) var standard = Log("", logLevel: .off)

/// Static instance used for helper methods.
open class var instance: Log {
return standard
}
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this is possibly still confusing for people using the old

class NetworkLog: Log {} approach, because NetworkLog.instance == Log.instance which means that NetworkLog.logLevel = .off still manipulates Log.logLevel.

I don't know if there's a good alternative approach here because there is no class var storage, so at a certain point you'll always have to reach for a static instance that is shared among all subclasses. So maybe it's best to just not even have any open override points that could potentially point to the same static instance, and just make it extra clear that setting Log.logLevel or NetworkLog.logLevel sets it for all instances and subclasses since they all refer to the same static variable.

Would also maybe suggest naming this singleton shared

Copy link
Author

Choose a reason for hiding this comment

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

That's fair, I was attempting to still allow for a similar class NetworkLog: Log {} approach, but giving a way to override which instance gets used from the static functions. Where I ended up, is a little more verbose.

    class NetworkLog: Log {
        static var networkLog = Log("Network")

        override class var instance: Log {
            return networkLog
        }
    }

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah yeah. In that case it would be nice if there was a way to force "holding it properly", and enforce this override for subclasses, but that isn't really possible either

Copy link
Author

Choose a reason for hiding this comment

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

I haven't found a good way to do that yet :-/

@jmagnini jmagnini force-pushed the feature/justin/logging branch 3 times, most recently from be399c7 to 14bad81 Compare May 26, 2021 22:07
@jmagnini jmagnini force-pushed the feature/justin/logging branch 2 times, most recently from 9b105c3 to 220b0ff Compare May 27, 2021 15:46
@jmagnini jmagnini marked this pull request as draft May 27, 2021 19:58
@jmagnini jmagnini marked this pull request as ready for review May 27, 2021 22:20
@jmagnini jmagnini requested a review from colejd May 27, 2021 22:21
…alLogHandler if desired.

Add export() to LocalLogHandler as well.
@jmagnini jmagnini removed the request for review from colejd September 16, 2021 17:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Make logLevel an instance variable instead of static
3 participants