Log File Highlighter
A Visual Studio Code extension for adding color highlighting to log files. It is based on standard conventions for log4net log files but it's general enough to be useful for other variations of log files as well. The colors are customizable but by default the current color theme's colors are used.
An earlier version of the syntax highlighting definition from this extension was included into Visual Studio Code version 1.20. If the default behavior of log files in VS Code is good enough for you, then you don't need this extension. However, if you want additional highlighting, customizable patterns and time duration calculation, then keep reading :-)
The extension associates with
.log files and applies coloring to the different elements in the file, for example:
Dates and times in ISO format, such as
Dates and times in some culture specific formats
Log level, such as
Numeric constants, such as
Standard .Net constants
String constants, enclosed in single or double quotes. Examples:
.Net exception type names, i.e. word ending with
Exception, such as
.Net exception stack traces, i.e. lines starting with whitespace characters, followed by
at, for example:
System.NullReferenceException: Object reference not set to an instance of an object.
at MyClass.DoSomethingElse(string foo)
Namespaces (sequences of alphanumeric and dot characters). Useful to identity namespace qualified type names, for example.
Visualization of Time Duration
If you select multiple lines of a
a status bar item is displayed that indicates how many days, hours, minutes, seconds and milliseconds you have selected.
Customizing the colors
By default, this extension reuses existing theme colors in order to adapt to the user's preferences. Sometimes this will result in colors that may not make complete sense in the log file context (for example
ERROR might be green and
DEBUG red, when it would be more appropriate to have it the other way around).
For this reason, the extension defines a special set of grammar tokens that can be used to override the colors of the default log items:
To override the color for one of these, use the
editor.tokenColorCustomizations user setting, which was introduced in Visual Studio Code 1.15. Here's an example that forces errors to be red and bold and warnings to be orange, regardless of what color theme is used:
Defining custom highlighting patterns
The extension also supports defining custom patterns to be highlighted. This can be useful to make the extension compatible with the log levels of an otherwise unsupported logging framework or to highlight domain specific patterns, or just about anything else.
The patterns are defined in the user settings like in this example:
foreground - [Optional] The color to use as foreground color for the matched pattern. Use hex colors or predefined VS Code colors.
background - [Optional] The color to use as background color for the matched pattern. Use hex colors or predefined VS Code colors.
background are optional individually but at least one of them must be set for the custom pattern to be activated.
Tip: By only setting the
background a custom pattern can be combined with the built-in patterns that control the foreground color. This is shown in the last pattern in the example above.
To make VS Code treat other file extensions than the default
.log as log files, add the following to the user settings:
The example above associates extensions such as
.log.2 with the Log File highlighter extension.