48

In a typical color terminal, there are escape sequences that one can use to print text in different colors. Typically there are 8 colors available. I tried using the standard, ANSI escape sequences for this in NSLog, but no dice. It does not support by that mechanism.

Is there a different way to print to the console (log) in color using NSLog?

Thanks.

  • Introduction of [CocoaLumberjack](https://github.com/robbiehanson/CocoaLumberjack) in this [blog](http://deusty.blogspot.com/2012/05/does-your-xcode-do-this.html). – ohho May 24 '12 at 10:10
  • Use the [Xcode Logger Library](https://github.com/codeFi/XcodeLogger). It's a simple-to-implement, colorful, flexible, customizable and faster NSLog replacement. – Razvan Jul 01 '15 at 11:39

4 Answers4

98

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

Lena Schimmel
  • 6,934
  • 5
  • 41
  • 56
  • 1
    Amazing !!, You saved my time. This is really simple & powerful than using coloured loggers. Thank You (up voted) – Thilina Hewagama Dec 16 '13 at 05:05
  • 27
    to Type emoji on Mavericks - Command + Control + Space, on Mountain Lion - option + command + T – Thilina Hewagama Dec 16 '13 at 05:07
  • Super useful if you write your own logging class that has log levels. Love having a dude checking passports for my warning logs. – Allison Oct 13 '14 at 21:24
  • Very funny and also useful! – ChildhoodAndy Jun 27 '15 at 06:00
  • I have tried to use the warning symbol, but when printing in the console, it does not look like the one in the Apple Color Emoji, but ⚠ – lmiguelvargasf Jun 30 '15 at 17:09
  • WARNING: If you decide to do this and use emojis to mark different kinds of logging levels, etc, just know that it will make logging to console much much slower and will result in buggy behavior when using debugger commands (e.g. autocomplete menu flickers constantly rendering it unusable sometimes). I think it has something to do with the console text layout engine not being built with emoji in mind so when it mixes fixed width fonts and emoji is screws it up. – Beltalowda Aug 28 '17 at 21:15
8

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.

ayoy
  • 3,755
  • 17
  • 20
  • What I was asking was whether this is actually true, that Xcode's debugging console offers no color capability. So far the answer seems to be yes. –  Jan 25 '12 at 16:33
  • 3
    Oh well, then I have no clue, but it seems so. For sure it's not an issue with `NSLog`, since Xcode's console doesn't color `printf()` either... – ayoy Jan 25 '12 at 16:43
  • 1
    I didn't do that :) I just located the binary and ran it in Terminal. And it was MacOS X binary. Only now I noticed 'ios' in this question's tags, sorry for the confusion... This however allowed to verify `NSLog` capabilities for coloring the output. – ayoy Jan 25 '12 at 17:40
  • 2
    You can choose the terminal for output by editing your scheme > options > Use Terminal. – capikaw Feb 08 '17 at 17:10
  • In Swift, `\e` causes the build-time error "_Invalid escape sequence in literal_" – Cœur Dec 10 '18 at 07:28
7

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.

djromero
  • 19,281
  • 4
  • 67
  • 67
6

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

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:

Besi
  • 22,215
  • 23
  • 120
  • 205