4

I would like to setup a new language definition for log files with a certain format. I've been scouring the interwebs for a tutorial that actually works, and I've yet to find one. I've tried creating sublime-syntax files, AAAPackageDev files (both JSON and YAML), plus one other syntax package that I can't remember the name of -- none of them seem to work, with some of them leaving out important details, like where to save files, how to get the syntax definition to appear as an option (I suspect this is related to save location), or how the keyword.other.joelog scope relates to the keyword.other theme color (do you just drop the last bit? does the last bit have to match the scope set out in the header?). It's been very frustrating.

At its simplest, I'd like to take the following file and highlight it:

2015-11-25 14:35:11 [LOG] Blah Blah Blah
2015-11-25 14:35:11 [LOG] some log statement
2015-11-25 14:35:11 [LOG] some other log statement
2015-11-25 14:35:11 [DEBUG] some embedded filename: [[ /path/to/file ]]
2015-11-25 14:35:11 [INFO] .............. blah blah ..............
2015-11-25 14:35:11 [DEBUG] <<PASS>> Directory not found: [[ /some/dir/name ]]
2015-11-25 14:35:11 [ERROR] <<FAIL>> Directory found: [[ /some/other/dir/name ]]
2015-11-25 14:35:11 [WARNING] some strange condition occurred

Ultimately I'd like syntax definitions similar to the following:

  • LOG lines should be "normal" text
  • DEBUG lines should be treated entirely as comments
  • ERROR lines should be treated as something else (e.g. a variable)
  • INFO lines as normal except the word INFO should be highlighted as something (e.g. a keyword)
  • WARNING similar to either ERROR or INFO
  • have embedded strings in the format of [[ some string ]] highlighted as a string, irrespective of the type of line it's on
  • have <<PASS>> and <<FAIL>> highlighted with different colors, irrespective of the type of line they're on

I'd be happy getting any of this to work when manually selecting the mode, and especially happy if I could have the mode selected based on the full filename (it would be selected based on a prefix, rather than just a suffix, e.g. joelog-20151126-110719.log keying off of both the 'joelog' prefix and the 'log' suffix).

Is this possible to do? I can write this in JSON, YAML, XML, Martian -- any language or style definition would be fine, if only I knew the rules.

Edit: Sorry, I meant to include the pages I've tried to follow:

With sublime-syntx files I couldn't figure out how to get it to show up as an option. With AAAPackageDev files, I could get my new syntax definition to show up, but it never highlighted anything, even when doing nothing more than following the tutorial (abandoning all of my own desires, just trying to get something to highlight in any fashion).

Thanks!

1

2 Answers 2

3

I started all over again and nothing worked...until I deleted all of my files and attempts, and then started yet again from scratch. Something in those files was preventing other definitions from being read in, maybe? No clue, but now it's working, finally.

The success I had was with PackageDev. Here's what I came up with (it's still in progress):

# [PackageDev] target_format: plist, ext: tmLanguage
---
name: JoeLog
scopeName: source.joelog
fileTypes: [joe]
uuid: 0fb395f8-9fb7-41c2-8b56-51f971de8505

patterns:
- match: ^\d+-\d+-\d+ \d+:\d+:\d+ \[DEBUG\] (<<PASS>>).*$
  name: comment.joelog
  captures:
    '1': {name: constant.other.symbol.joelog}
- match: ^\d+-\d+-\d+ \d+:\d+:\d+ \[DEBUG\] (<<FAIL>>).*$
  name: variable.joelog
- match: ^\d+-\d+-\d+ \d+:\d+:\d+ \[DEBUG\].*$
  name: comment.joelog
- match: ^\d+-\d+-\d+ \d+:\d+:\d+ \[ERROR\].*$
  name: variable.joelog
- match: ^\d+-\d+-\d+ \d+:\d+:\d+ \[LOG\].*$
  name: support.class.joelog
- match: ^\d+-\d+-\d+ \d+:\d+:\d+ \[INFO\].*$
  name: support.function.joelog
- match: ^\d+-\d+-\d+ \d+:\d+:\d+ \[WARNING\].*$
  name: keyword.other.joelog
- match: \[WARNING\]
  name: keyword.other.joelog
- match: ^\d+-\d+-\d+ \d+:\d+:\d+ \[FATAL\].*$
  name: invalid.illegal.joelog
- begin: ^\d+-\d+-\d+ \d+:\d+:\d+ \[STACK TRACE\].*$
  end: ^\s*$
  name: invalid.illegal.joelog
...

I still don't know half of what I'm doing, but at least something is working. I'd like to be able to color in <<PASS>> and <<FAIL>> without having to duplicate the DEBUG definition, as those strings may appear on other lines, and I don't want to duplicate all of them, but I haven't figured out how to do that, yet (any pointers would be welcome).

Scopes were picked out because of the colors in use in my chosen theme, but that's probably a bad idea, and I should go with scopes that makes sense from a contextual standpoint. But I'll leave that for another day. The TmTheme Editor was a big help in seeing what scopes are defined in my theme and what the actual scope names were.

I used the ApplySyntax package (available via Package Control) to select this new syntax based on the full file name, not just the extension.

0
2

Here is a list of commonly used scopes that I compiled by re-engineering some color-themes and using your helpful link to the TmThemeEditor:

# Common generic scopes used in sublime-syntax / color-scheme
comment
comment.line
constant
constant.character
constant.character.escape
constant.language
constant.numeric
constant.other
constant.other.symbol
entity
entity.name.class
entity.name.filename
entity.name.function
entity.name.tag
entity.name.type.class
entity.other.attribute-name
entity.other.inherited-class
invalid
invalid.deprecated
keyword
keyword.control
keyword.control.import
keyword.operator
keyword.other
punctuation
punctuation.definition.string.begin
punctuation.definition.string.end
punctuation.definition.tag
punctuation.definition.tag.begin
punctuation.definition.tag.end
punctuation.definition.variable
storage
storage.modifier
storage.type
storage.type.class
storage.type.function
string
string.regexp
support
support.class
support.constant
support.function
support.other.variable
support.type
variable
variable.function
variable.language
variable.other
variable.parameter

Not the answer you're looking for? Browse other questions tagged or ask your own question.