7

Is there a syntax highlighting for Java stack traces. Any tips about people editing posts looking at that?

3
  • 8
    Syntax highlighting for a stack trace? Have you ever seen a stack trace colored? Pretty sure we just use plain text for those... Aug 7, 2013 at 13:03
  • Is this a question about syntax highlighting on Stack Exchange or about syntax highlighting in general? Aug 7, 2013 at 13:07
  • 1
    @Crowie - yep. If prettify doesn't have it, neither do we.
    – Oded
    Aug 7, 2013 at 13:13

1 Answer 1

14

Is there syntax highlighting for Java stack traces? Yes and no.

You can add colour to your stack trace via the features of Google Prettify's standard Java syntax highlighting, which is what Stack Exchange uses. You'll get class and package colour added to your trace but no specific functionality that handles to traces and formats them for reading.

An example of what you can get:

log4j:ERROR setFile(null,true) call failed.
java.io.FileNotFoundException: /log/t2-console.log (No such file or directory)
    at java.io.FileOutputStream.open(Native Method)
    at java.io.FileOutputStream.<init>(FileOutputStream.java:212)
    at java.io.FileOutputStream.<init>(FileOutputStream.java:136)
    at org.apache.log4j.FileAppender.setFile(FileAppender.java:294)
    at org.apache.log4j.RollingFileAppender.setFile(RollingFileAppender.java:207)
    at org.apache.log4j.FileAppender.activateOptions(FileAppender.java:165)
    at org.apache.log4j.config.PropertySetter.activate(PropertySetter.java:307)
    at org.apache.log4j.xml.DOMConfigurator.parseAppender(DOMConfigurator.java:295)
    at org.apache.log4j.xml.DOMConfigurator.findAppenderByName(DOMConfigurator.java:176)



How did you do that?

It should just work. But since you're here reading there are two ways to make it kick in:

  1. Automatically: Tag the question as Java. Stack traces will add some syntax colouring code if there is a Java tag on it.
  2. Explicitly: If for some reason you wanted it to occur in isolation, maybe without the Java tag or for some other reason it just isn't working, you can explicitly convert your stacktrace into a Java code block.


How do I make it kick-in explicitly? I.e. How do I do section based Java syntax highlighting?

Like this:

  • Indent what you want formatted by four spaces,
  • Paste the stack trace, and
  • Put one of the following above it with appropriate line breaks and spacing:

<!-- language: lang-java -->


"There's no accounting for taste"

Up to you if you like the colour. I don't mind it but I'm starting to prefer plain text code. I'd prefer something better, something specifically for a trace. But those two options - nothing or something - are what you're going to get for now on SE.

Personally I think it helps to put colour onto the class names and separate out the packages. To me and others it is going to be marginal but I think the benefits are along the lines of what you get when a question is edited for grammar. It can help more quickly identify the class, package or module that a problem is based within. It's something Eclipse and IntelliJ have seen fit to add. IMO if that makes the question and answer more useful, that's probably going to be a good thing. However, as I said, it's up to your taste but all you get is something or nothing.


More on the No part of this answer

"If prettify doesn't have it, neither do we."

As I alluded to, I don't think this is an intentional feature and as mentioned in the comments here, stack traces here are often left as plain text. If it's something you're keenly interested in, something you'd like to see brought into Stack Exchange or something you'd like to work on Prettify is a good place to start. If nothing else, it could be a good open-source project.

There are a few discussions on features of syntax highlighting on Stack Exchange if you look at the related questions area to the right of the question/answer here. The commonly directed one that is most commonly thrown up is "There's a bug in the syntax highlighting".

0

Not the answer you're looking for? Browse other questions tagged .