skip to Main Content

These logs are from any external sources. I use VS Code as a log viewer. I open a file with a *.log extension, or create a new file and select log language (ctrl+k, m).

I cannot find how to set colors for log levels when this level has a shorter name.

As you can see below, VS Code applies color for "INFO" or "INFORMATION", but not for "INF" etc.

log levels

2

Answers


  1. For any text views in VS Code using syntax highlighting based on its builtin "log language", which includes editors for text files with the .log extension, and output channels which set their language using the languageId argument of createOutputChannel, the syntax highlighting will be based on the grammar as defined by VS Code’s "log language"’s textmate grammar file.

    To get different behaviour, you have a couple options:

    • Modify the definition of that language. Unfortunately, that file is rather internal to a VS Code installation, so you’d have to dig into the files of your VS Code installation and modify it there to get that, and VS Code would likely then give a corruption warning saying that it detected its files have been tampered with.

    • Create a custom VS Code extension that is basically a copy of VS Code’s builtin log language extension, but modify it to suit your own needs. Then use the files.associations setting to make files with the .log extension use your language’s ID instead of the ID of VS Code’s builtin one. Note that this will have no effect for output panels that set their language to VS Code’s builtin one- you have no control over that.

    • Or you could open a feature request issue ticket asking that the textmate grammar be modified to add support for such token matches. If you do that, please comment here with a link to that issue ticket for posterity.

    Login or Signup to reply.
  2. Try the Log File Highlighter extension. It can be customized to handle the different spellings you are using. For example, this setting (in your settings.json):

    "logFileHighlighter.customPatterns": [
      {
          "pattern": "INFORMATION|INFO|INF",  // more specific works best
    
          // to restrict with a positive lookahead of the ] character
          // "pattern": "(INFORMATION|INFO|INF|info)(?=\])",
          "foreground": "#22aa22"
      },
      {
          "pattern": "ERROR|ERR",
          "foreground": "#f00"
      },
      {
          "pattern": "DEBUG|DBG",
          "foreground": "#00f"
      }
    ]
    

    produces this:

    log level colors with Log File Highlighter

    There are many style options besides the foreground color and any text can be customized. It also colorizes the logs in the Output view.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search