Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Any way to print in color with NSLog?

Tags:

ios

colors

nslog

You can use Apple Color Emoji to add some color to your Log output like this:

if ([self isKindOfClass:[UITableViewController class]])
    NSLog(@"πŸ’™ Table View controller Will appear: %@", NSStringFromClass([self class]));
else  if ([self isKindOfClass:[UINavigationController class]])
    NSLog(@"πŸ’œ Navigation controller Will appear: %@", NSStringFromClass([self class]));
else
    NSLog(@"πŸ’› View controller Will appear: %@", NSStringFromClass([self class]));

Because the above code might by displayed incorrectly on non-OS-X-platforms, I attached a screenshot of XCode, showing the code and log output:

Screenshot from X Code with Apple Color Emoji in code and log output


A line like this works for me:

NSLog(@"\e[1;31mRed text here\e[m normal text here");

(More colors available here)

Note that you have to actually watch your output in regular console, as Xcode debugging console doesn't seem to support coloring.


You can colorize your NSLog output using this Xcode plugin: https://github.com/DeepIT/XcodeColors

I wrote a few lines with my quick setup instructions and simple custom macros.

Update: I'm using now NSLogger with color support. Much powerful.


Ayoy's approach seems to work in general in a command line based app with printf:

  • http://i.minus.com/iT5kYgSscw9fy.png

However, I don't think this works with the built-in Xcode console:

This:

NSLog(@"\e[1;31mRed text here\e[m normal text here");

... results in this:

  • http://i.minus.com/j4hu1lr03eWsP.png