1

I'm pretty new to powershell scripts. I'd like to display a log file in a gridview using out-gridview.

Let's say I have a very simple script like

$logFile = "logs\myCoolLogFile.log";
gc -wait $logFile | Out-GridView -Wait

This displays each row in a single cell. My log is formated like this:

LEVEL TIMESTAMP LOGGERNAME [THREAD]: MESSAGE

Each entry is separated by one or more spaces (but I can change it easily to be separated by one tab character if it was more suitable). The message may contain additional spaces. The previous entries do never contain spaces.

I'd like to have different columns for LEVEL, TIMESTAMP etc. But I'm not sure how to achieve this. Any help is appreciated.

Edit: As @jisaak requested, here is an example of my log with a multi line message:

WARN    09:53:49.938    n.s.e.CacheManager  [StartStop-Thread]  Creating a new instance of CacheManager using the diskStorePath "C:\temp" which is already used by an existing CacheManager.
The source of the configuration was net.sf.ehcache.config.generator.ConfigurationSource$DefaultConfigurationSource@48666bf4.
The diskStore path for this CacheManager will be set to C:\temp\ehcache_auto_created_1461052429938.

Edit2: Now, that I think about it, I guess it would be acceptable if the lines which do not contain enough columns would be skipped altogether. But I do need help for that behavior as well.

samjaf
  • 1,033
  • 1
  • 9
  • 19
  • Well, you could parse the log file using a regex but maybe you can control the output of your logs and use a xml or json formatter? – Martin Brandl Apr 19 '16 at 09:53

1 Answers1

1

You can use the ConvertFrom-Csv cmdlet to convert your log:

$logFile = "logs\myCoolLogFile.log";
gc $logFile |  ConvertFrom-Csv -Delimiter ' ' | Out-GridView

Why are you using the -wait switch on the Get-Content cmdlet?

Martin Brandl
  • 56,134
  • 13
  • 133
  • 172
  • I thought this was necessary in order to update the content of the gridview once the file itself is updated. Am i wrong about this? – samjaf Apr 19 '16 at 07:50
  • Your solutions works almost like a charm (I converted the log format to tab characters as delimiters). But the log file may contain multi line messages. Since each new line is printed without leading tab-characters into the file, it is recognized as first column and printed inside the "Level" column. Do you have any idea how this can be avoided? – samjaf Apr 19 '16 at 08:02
  • hm, a new line is usually a new record so this could be tricky. If you share an example, we can maybe find a workaround. – Martin Brandl Apr 19 '16 at 08:09
  • Thank you a lot for your input. I considered changing the log format to json or xml. But as the out-gridview commandlet seems to lag an autoscroll feature I dropped the script altogether. Nevertheless you answered my question perfectly :) – samjaf Apr 20 '16 at 04:33