OSLog is a unified logging system for the reading of historical data.

OSLog Documentation

Pinned Posts

Posts under OSLog tag

63 Posts
Sort by:
Post not yet marked as solved
4 Replies
6.1k Views
Is it possible to reduce the verbosity of logs from os_log when debugging an application? Specifically, can I filter to specific subsystems and / or exclude DEBUG logs using configuration?
Posted Last updated
.
Post marked as solved
3 Replies
754 Views
When running the code below, only the first entry gets logged. Structured logging seems not to work when there are emoticons or object descriptions in the message. Is this by design? let logger = Logger(subsystem: "TestSystem", category: "TestCategory") logger.log("👍") // Result = 👍 let someEmoji:String = "👎" logger.log("\(someEmoji)") // Result = empty log line let someObjectDescription:String = String(describing:self) logger.log("\(someObjectDescription)") // Result = empty log line
Posted Last updated
.
Post not yet marked as solved
2 Replies
591 Views
I'm trying to create a macro that adds the file and line to a string that I use with OSLog. However, I only get warnings that either the String is not in the format of OSLogMessage or not an interpolated string. The Macro looks like this at the moment: public struct LocMacro: ExpressionMacro { public static func expansion( of node: some FreestandingMacroExpansionSyntax, in context: some MacroExpansionContext ) -> ExprSyntax { guard let argument = node.argumentList.first?.expression.as(StringLiteralExprSyntax.self)?.segments else { fatalError("compiler bug: the macro does not have any StringLiteralExprSyntax arguments.") } guard let file = context.location(of: node)?.file.as(StringLiteralExprSyntax.self)?.segments, let line = context.location(of: node)?.line.as(IntegerLiteralExprSyntax.self) else { fatalError("compiler bug: the macro is unable to retrieve file and line numbers") } return "\"\(argument) - \(file):\(line)\"" } } and here the exposed macro: @freestanding(expression) public macro loc(_ text: String) -> String = #externalMacro(module: "LxoMacrosMacros", type: "LocMacro") I want to use it like this: import LxoMacros import OSLog let logger = Logger() let someNumber = 17 logger.debug(#loc("Working with some number \(someNumber)")) which should expand to: logger.debug("Working with some number \(someNumber) - MyFile.swift:8") Is there a way to change the macro so that it returns the correct type? When expending the macro it does show the right string, which works with print() and so on, but assuming since the Logger uses some sort of compiler check as well, it doesn't seem to work together as expected.
Posted Last updated
.
Post not yet marked as solved
7 Replies
1k Views
Since this api requires &__dso_handle instead of the standard file/line/func, I had to modify my entire log system to pass this down from the macro call sites. I have many callbacks that typically just forward data from other C++ libraries that never supply a dso_handle. so it's great how this logging system breaks most logger systems and doesn't have a warning level to match fault/error. I have the forwarded threadName, timestamp, etc and no where to store that in os_log. syslog was more powerful and clear than os_log, but I'm sure it's now too late to head down a more reasonable path.. So I pass the &__dso_handle all the way to the log command and hand it into the macro #define my_os_log_with_type(dso, log, type, format, ...) __extension__({ \ os_log_t _log_tmp = (log); \ os_log_type_t _type_tmp = (type); \ if (os_log_type_enabled(_log_tmp, _type_tmp)) { \ OS_LOG_CALL_WITH_FORMAT(_os_log_impl, \ ((void*)dso, _log_tmp, _type_tmp), format, ##__VA_ARGS__); \ } \ }) Logger.mm // This doesn't work, logging the dso from the callsite. No file/line. my_os_log_with_type(entry.dso, os_log_create( "com.foo", entry.tag), logLevel(entry.level)), "%{public}s", text ); // This does work, but who wants to jump to the forwarding log implementation? os_log_with_type(os_log_create( "com.foo", entry.tag), logLevel(entry.level)), "%{public}s", text );
Posted
by Alecazam.
Last updated
.
Post not yet marked as solved
4 Replies
491 Views
Howdy all, I'm in the process of eliminating some compiler warnings in a legacy project, and have come across a compiler warning for the usage of extern void DebugStr(ConstStr255Param debuggerMsg), defined in MacTypes.h as part of CarbonCore-769-1. I've looked through Apple's documentation for Cocoa and the MacOSX13.1 SDK but haven't found any leads on what I could/should replace it with. Can anyone point me in the right direction? Thanks!!
Posted Last updated
.
Post not yet marked as solved
1 Replies
570 Views
Hello folks, I would like to incrementally retrieve logs of my iOS App from the Unified Logging System using OSLogStore. First I initialize the store store = try OSLogStore(scope: .currentProcessIdentifier) Second I set a position from which I want the logs using either : position = store.position(timeIntervalSinceLatestBoot: 0) // to get the logs from the launch of the App position = store.position(date: Date().addingTimeInterval(-10)) // to get the logs of the last 10 seconds Third I get the entries in the store from the position store.getEntries(at: position) Issue Whatever the position I set using from OSLogStore: position(timeIntervalSinceLatestBoot:) position(date:) position(timeIntervalSinceEnd:) All the logs are retrieved. I cannot get only the subset requested by the timeframe I specified Sample A sample LogScreen that has 2 buttons is attached to the post 1 to fetch logs since the last time they have been fetched 1 to add a log The code has been simplified to ease the comprehension of the case, please be indulgent to the shortcuts that have been taken. LogScreen.swift References https://developer.apple.com/documentation/oslog/oslogstore https://developer.apple.com/forums/thread/705868 https://developer.apple.com/videos/play/wwdc2023/10226/ Environment XCode 15b7 iPhone 14 Pro / iOS 17
Posted
by jobob.
Last updated
.
Post not yet marked as solved
3 Replies
502 Views
When the number of log outputs over a certain amount, the structure log output is suspended. The os_log_debug code runs without error, but the Xcode console has no output. When use OS_ACTIVITY_DT_MODE=1 turnoff structure log, the output is normal. Does structure log in Xcode 15 beta 6 has bugs, or the structure log has upper limit in Xcode console output ?
Posted
by lsw-lsw.
Last updated
.
Post not yet marked as solved
2 Replies
1.2k Views
My problem is similar to this post but switching to an iOS 17 simulator does not fix the issue. As far as I know there should be no OS_ACTIVITY_DT_MODE set anywhere in my project but I'm not sure exactly where to check. I've tried in Beta 6 and 7 so far and I've set all of my minimums to iOS 17.
Posted
by dav_es.
Last updated
.
Post marked as solved
1 Replies
1.5k Views
Hi! I am getting all debug output in text only format, without structure or color. I only noticed now after watching the respective WWDC session in Xcode 15 beta 8, but I am pretty sure this was the case in the previous betas as well. Is the new structured debug console activated by some setting I am overlooking?
Posted
by RK123.
Last updated
.
Post not yet marked as solved
3 Replies
1.2k Views
I have an iOS application which is developed with C++, and my physical iPhone is connected to my Mac. While running the app via Xcode, I can easily view the printouts within the Xcode console. However, upon manually executing the app on the iPhone and utilizing the command "idevicesyslog -d -p myAppName" on my Mac the terminal, I notice that only specific logs generated by the .mm files are visible. And I'm unable to access the remaining traces, macros, and other details from the .h or .cpp files. Is there a method to ensure that all the logs I observe in the Xcode console are also displayed in "idevicesyslog"? Alternatively, are there any technique to show all the printout from .cpp and .h files.
Posted
by ken123.
Last updated
.
Post marked as solved
3 Replies
1.1k Views
Hi there! Sorry in advance, this is going to be a long post of Apple developer pains which I want to share with you, and, hopefully, find the answer and help Apple become better. I'm at the very beginning of my new and exciting personal project which (I hope) may one day feed me and be my daily source of inspiration. I'm not a newbie in Apple development nor am I a senior-level developer — just a fellow developa'. Here's the problem I bring to you — why Apple promotes Unified Logging System and recommends using it as the primary way to implement logging in 3rd-party apps? No doubt, OSLog is a great, secure, efficient, and centralized way to gather diagnostics information, and I, starting my new project, am itching to choose exactly this 1st-party logging infrastructure. This decision in theory has a number of benefits: I don't have to depend on 3rd-party logging frameworks which may eventually be discontinued; I have extensive documentation, great WWDC sessions explaining how to use the framework, and stackoverflow answers from the whole Apple dev community in case I experience any troubles; I have this cool Console.app and upcoming Xcode 15 tools with great visualization and filtering of my logs; It's quite a robust and stable infrastructure which I may restfully rely on. But... the thing is there's this big elephant in the room — this API is non-customizable, inconvenient, and hard to use in terms of the app architecture. I can't write my own protocol wrapper around it to abstract my domain logic from implementation details or just simplify the usage at the call site. I can't configure my own format for log messages (this is debatable, since Console.app doesn't provide "***** strings" as Xcode 14 and earlier, but still). And what's most important — I can't conveniently retrieve the logs! I can't implement the functionality where my user just taps the button, and the logs are sent on the background queue to my support email (eskimo's answer). They would have to go through this monstrous procedure of holding volume buttons on the iPhone, connecting their device to the Mac, gathering sysdiagnose, entering some weird Terminal commands (jeez, these nerdy developers...), etc. If it ever succeeds, of course, and something doesn't go wrong, leaving my user angry and dissatisfied with my app. Regarding the protocol wrapper, I can't do something like this: protocol Logging { var logger: Logger { get } func info(_ message: OSLogMessage) } extension Logging { var logger: Logger { return Logger( subsystem: "com.my.bundle.id", category: String(describing: Self.self) ) } func info(_ message: OSLogMessage) { logger.info(message) } } class MyClass: Logging { func someImportantMethod() { // ... self.info("Some useful debug info: \(someVar, privacy: .public)") } } I've been investigating this topic for 2 days, and it's the farthest I want to go in beating my head over how to do two simple things: How to isolate logging framework implementation decision from my main code and write convenience wrappers? How to easily transfer the log files from the user to the developer? And I'm not the only one struggling. Here's just one example among hundreds of other questions that are being asked on dev forums: https://www.hackingwithswift.com/forums/ios/unified-logging-system-retrieve-logs-on-device/838. I've read almost all Apple docs which describe the modern Unified Logging System, I've read through eskimo's thread on Apple Developer Forum about the API, but I still haven't found the answer. Maybe, I've misperceived this framework and it's not the tool I'm searching for? Maybe, it focuses on different aspects of logging, e.g. signposting, rather than logging the current state of the app? What am I missing?
Posted
by ostaptyv1.
Last updated
.
Post marked as solved
5 Replies
2.9k Views
Using the latest Xcode 15 Beta 7: In an existing project, I can't get structured logging to work as described. For new projects, it works fine but I must have some setting or value in an existing project that prevents it from working. My simple text case: In an existing project file: @main struct LuarasBooksApp: App { var body: some Scene { WindowGroup { Text("hi") .onAppear { Logger(subsystem: "test", category: "test").error("help me") } } } } The debug console looks like this: That same code in a new project for the same Xcode 15 Beta 7 looks correct: Are there any settings, variables, etc... related to logging or the console that might cause this difference? In the existing project it makes no difference to toggle the various metadata options on and off. Thanks!
Posted
by mrsdizzie.
Last updated
.
Post not yet marked as solved
3 Replies
995 Views
Hi, I'm currently developing Apple Pay In-App Provisioning and have encountered an issue with viewing logs necessary for debugging. I've followed the instructions listed in https://download.developer.apple.com/iOS/iOS_Logs/Wallet_Logging_Instructions.pdf and have installed the relevant wallet profile. After repeating the issue, and opening up the sysdiagnose file on my mac, the logs that i'm interested in is still hidden with private tags. The device I'm using to develop on is an iPhone 11, runnning on iOS 16.5. The version of Xcode is 14.3. I am also using a sandbox account on the device if that is relevant to the issue. Below are screenshots of the logs I'm interested in. Any help in solving this issue will be greatly appreciated.
Posted
by RaiKouz.
Last updated
.
Post marked as solved
2 Replies
404 Views
Hi, I'm on MacBook Pro M2, macOS 13.3.1. I'm writing a feature that need to run before login. After some research I found PreLoginAgents sample code: https://developer.apple.com/library/archive/samplecode/PreLoginAgents/Introduction/Intro.html Followed README, installed pre-built agent and SSH to Macbook, then run: sudo syslog -c 0 -i syslog -w volia! SSH prints agent log, it is working. But if I build code with XCode, there is no log. I tried following changes with no luck: change LogManager acl log to os_log_info(OS_LOG_DEFAULT, "%s", [str UTF8String]), no log change to syslog(LOG_INFO, "%s", [str UTF8String]), has log! But change min deploy target from 10.9 to 11.0, syslog print no log What is the proper way to print PreLoginAgents log? Thanks!
Posted Last updated
.
Post not yet marked as solved
2 Replies
447 Views
Note: I have read the Your Friend The System Log post, which was very helpful - thanks! I have a follow-up question. I have added support in our app for reading the log messages via OSLogStore. However, as you mention, the Debug and Info level messages do not show up when we read messages when not connected to a debugger. You mention two ways on iOS to change this configuration: Install a configuration profile created by Apple (all platforms). I assume all of these only work for the "com.apple.*" subsystems, correct? So this won't really work for 3rd-party apps like mine. Is this correct? Add an OSLogPreferences property to your app’s Info.plist (all platforms). Is there any official documentation for the OSLogPreferences Info.plist key? I was not able to find any. Does the value of "OSLogPreferences" follow the same dictionary structure described here? Thanks! Eric
Posted
by eric_noom.
Last updated
.
Post not yet marked as solved
1 Replies
324 Views
I'm experimenting with the (relatively) new Logger API after watching WWDC20 session "Explore logging in Swift", and I don't see a default reduction of the values that was discussed in the talk. When I run this code, let i = A(a: "aaaa", b: 111) logger.error("Password: \(i)") // privacy: .auto logger.error("Password: \(i, privacy: .public)") logger.error("Password: \(i, privacy: .private)") logger.error("Password: \(i, privacy: .private(mask: .none))") logger.error("Password: \(i, privacy: .private(mask: .hash))") I get this output in both Xcode console and Console.app: 2023-08-20 22:55:57.373918+0300 MyApp[75828:757479] [MyCategory] Password: a: aaaa, b: 111 2023-08-20 22:55:57.373988+0300 MyApp[75828:757479] [MyCategory] Password: a: aaaa, b: 111 2023-08-20 22:55:57.374014+0300 MyApp[75828:757479] [MyCategory] Password: a: aaaa, b: 111 2023-08-20 22:55:57.374032+0300 MyApp[75828:757479] [MyCategory] Password: a: aaaa, b: 111 2023-08-20 22:55:57.374055+0300 MyApp[75828:757479] [MyCategory] Password: a: aaaa, b: 111 My assumption is that when running the code from Xcode with debug build configuration the system generously opens up any private values so I can inspect them conveniently. But when I ran my code with the release config, I got the same output. However, I want to test what data will be stored on my users' devices when I write different privacy options Any ideas why this happens?
Posted
by ostaptyv1.
Last updated
.
Post not yet marked as solved
1 Replies
398 Views
Hello Folks, I am simply trying to learn to take client-side logs using Xcode or any easy-to-use tools. Please help. That way I can figure out if the issue is on the client side or not. For the server side, I use Charles proxy and I know how to use that. Any help is appreciated!
Posted
by anahad_a.
Last updated
.
Post not yet marked as solved
1 Replies
1.5k Views
I'm assuming it's possible to stream the device log to a file, any clues? I'm trying to debug an app installation issue I am having with iOS 17, and hunting through the Console log is just super noisy. I want to just attach the log so I can hand it over to my developers on the team, but I don't seem to be seeing a way to save the Console log I am looking at in Xcode. Basically I'm looking for the equivalent of an adb shell logcat +pipe to storage that Is not that hard to do on an Android. By any means possible? I've not even tried using LLDB, will it let me do this, if so any clues where to start?
Posted
by conradb.
Last updated
.
Post not yet marked as solved
1 Replies
447 Views
I've got a complex app extension and I'd like to be able to combine the logging from the app and the extension into a single file (for extraction/uploading etc.) I experimented adding the following code to both the app and the extension i.e. using the same subsystem for both app and extension logging. let subsystem = "mySubsystem" let logger = Logger(subsystem: subsystem, category: "main") logger.info("Log from within extension") // the version in the app says Log from within app do { let logStore = try OSLogStore(scope: .currentProcessIdentifier) let oneHourAgo = logStore.position(date: Date().addingTimeInterval(-3600)) let allEntries = try logStore.getEntries(at: oneHourAgo) let filtered = allEntries .compactMap { $0 as? OSLogEntryLog } .filter { $0.subsystem == subsystem } } The filtered content only contains logging from either the app or the extension. I suppose the issue might be the use of .currentProcessIdentifier as the scope, however that's the only scope that is defined. Is what is being attempted here possible? Is there any way to write to and extract a unified log for an app and app extension? If not, how about adding it, surely this would be something useful for people to be able to do
Posted
by mungbeans.
Last updated
.
Post not yet marked as solved
1 Replies
809 Views
Hi, I'm currently working on exporting logs from a user's phone. Let's say I want to include an option in the settings that allows the user to retrieve all logs recorded via Logger. I've found that it's possible to use OSLogStore to get logs recorded via Logger. It has initializer with the scope of the current process identifier OSLogStore(scope: .currentProcessIdentifier). This method works fine until the app is killed or crashes. How can I retrieve the persistent logs in a manner similar to the log collect command? We need to access logs from when the app was killed or crashed, and we don't have direct access to the user's phone.
Posted
by fineas.
Last updated
.