NSLog and You: Learning How and When to Use It

NSLog and You: Learning How and When to Use It

DebuggingiOS
Fix bugs faster! Log Collection Made Easy
START NOW!

NSLog is a very useful function call that iOS developers are able to utilize, and it is one of the most common development tools used to debug iOS apps. Among other things, NSLog can be used to check the value of variables, log any notes that you need to make, and check for errors when you do not have a debugger available. While NSLog is fairly handy, it is not a universal tool that can solve all of your programming problems. An experienced developer knows when NSLog is the best tool available to them, as well as the potential downsides of utilizing NSLog over other options. Beginner developers should strive to commit these facts to memory in order to maximize their efficiency with creating and improving apps.

What is NSLog

The NSLog function is a powerful tool for iOS app developers in Apple Objective-C and Swift. It allows app developers to output log messages to the console for valuable debugging information. By adding a single line of code, developers can easily print informative messages to track execution flow and troubleshoot bugs. NSLog also offers flexibility with log levels, allowing developers to control the detail of their messages. It supports string interpolation, making it easier to track variable values and data flow. However, it’s important to learn when to use NSLog to avoid performance issues. In conclusion, NSLog is a valuable resource for mobile app developers, allowing to have more debugging capabilities to find bugs faster and improving app quality.

When to Use NSLog

Determining exactly when it is best to use NSLog is not quite an exact science. The key idea is to find a balance between how much information you are going to need and how often you will need that information. When thinking about the balance between these two factors, it is crucial to keep in mind that it is better to have too much information than not enough. Under any circumstances, you want to be sure that you have at least the barebones minimum required to keep everything functioning.

Exactly what you want to use NSLog for will depend on what your current needs are. For example, you may want to view controller lifecycles in order to track user flow to your app. On the other hand, an app that still needs a bit of work done could use NSLog to monitor server requests and print error responses that result from those requests.

How to Use NSLog

Using NSLog for debugging you are is very simple, you just need to call NSLog Objective-C function like in this example:

int number = 25;
NSString *text = @"Apple";
NSLog(@"Number value is %d and text is %@.", number, text);

The first argument provided in the NSLog call specifies the format string. The NSLog function uses the same format string specifiers than printf function. You have the full list in the Apple Documentation.

The previous code snippet in Objective-C initializes an integer and a string, then prints them to the standard output console.

Advanced NSLog usage

Logging Complex Data Types and Structures

Besides basic string and integer logging, as we have already seen NSLog allows for logging complex data types using format specifiers, similar to the printf function. For example, %@ is used for Objective-C objects, and you can log arrays, dictionaries, or custom objects using this specifier.

Including Meta-Data in Logs

Sometimes you need more information that just the log context, especially when debugging. For example, you might want to include file names, line numbers, and function names in your logs. A custom macro like ALog(fmt, ...) can be defined to automatically include this metadata. For instance:

#define ALog(fmt, ...) NSLog((@"%s [Line %d] " fmt), __PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__);
ALog(@"Value of x: %@", x);

Logging Stack Traces

In case there’s a crash, logging a stack trace can be invaluable. Using NSThread‘s -callStackSymbols method, you can retrieve and log the current stack trace:

NSLog(@"%@", [NSThread callStackSymbols]);

What is the alternative to NSLog in Swift?

In Swift, the equivalent of Objective-C’s NSLog for logging or printing output to the console is print(). The print() function in Swift is straightforward and easy to use for basic console output.

Be A Superhero

Fend off bugs and keep your users safe.

Use Bugfender for Free Today

Downsides to NSLog

While NSLog is handy, it is not perfect. There are a few key issues that can cause problems some developers may consider too bothersome to make NSLog worth utilizing.

Performance

NSLog is synchronous, i.e. slow. Luckily, there are ways to disable NSLog and work around this particular issue. One method is to use a debug configuration and replace NSLogs with DLogs. This will cause debug messages to pop up while testing and debugging your app; when you release a beta or final version of the app the DLog lines become empty and will no longer do anything.

Start by finding the ‘yourProjectName-prefix.pch’ file and add three lines to the end of the .pch file:

#ifdef DEBUG
#define DLog(...) NSLog(__VA_ARGS__)
#else
#define DLog(...) /* */
#endif

Then test the difference between the debug version and release version of your app. If you no longer see any NSLog output in the release version, you have implemented this feature correctly. There are many options available for eliminating NSLog’s effects on performance, so do not panic if neither of these two options work for one particular project or another.

No Intelligence

NSLogs do not contain any kind of log classification. This makes it difficult to tell the difference between errors, warnings, and other information. Having tags and different log levels would be extremely useful, but that is simply not how NSLog works.

Remote Access

NSLog only works when your device is physically attached to your computer; there is not an option for remote access. This lack of functionality can be extremely frustrating for developers who are consistently on the go and do not always have the ability to sit down and get everything hooked up when they find themselves with a few extra minutes to work on their latest project.

Bugfender, a Better NSLog

Bugfender has many of the same features as NSLog, but with superior performance levels due to increased functionality. On top of being able to do everything that NSLog is capable of, Bugfender provides several additional features that NSLog does not offer. All logs are processed on a background thread, saving developers the trouble of having to disable NSLogs for beta or full release versions of apps. Bugfender is also able to acquire logs from remote devices, even if the app has been used offline. Furthermore, these logs feature tags and levels that make the files easy to navigate, allowing developers to locate error messages or other pieces of information with ease.

NSLog is handy and has been around for a very long time, but it is past its heyday and many developers are beginning to realize that it may be time to put old NSLog out to pasture. There are more modern technologies that can do NSLog’s same job better and Bugfender is at the forefront of these new, innovative solutions. Integrating and using Bugfender is a quick and simple process, so be sure to give it a try today! Once you see how much more it has to offer over NSLog, you will wonder why you ever waited to switch over.

Expect the Unexpected! Debug Faster with Bugfender
START FOR FREE

Trusted By

/assets/images/svg/customers/highprofile/intel.svg/assets/images/svg/customers/highprofile/deloitte.svg/assets/images/svg/customers/highprofile/schneider_electric.svg/assets/images/svg/customers/projects/slack.svg/assets/images/svg/customers/highprofile/axa.svg/assets/images/svg/customers/cool/ubiquiti.svg/assets/images/svg/customers/cool/riachuelo.svg/assets/images/svg/customers/projects/menshealth.svg

Already Trusted by Thousands

Bugfender is the best remote logger for mobile and web apps.

Get Started for Free, No Credit Card Required