mrodalgaard / language-log

Log file syntax highlighting in Atom
MIT License
42 stars 9 forks source link

Set grammar for any filename that contains the word "log" #1

Closed kankaristo closed 9 years ago

kankaristo commented 9 years ago

Thanks for a great package, this and tail are a perfect match.

While most do, some log files don't have the .log extension, so I put this in my init.coffee:

atom.workspace.observeTextEditors (editor) ->
  path = editor.getPath()?.toLowerCase()
  return unless path?

  contains =
    log: "source.log"

  # Match file name (if no grammar is set)
  if editor.getGrammar().name is "Null Grammar"
    for namePart of contains when path.indexOf(namePart.toLowerCase()) > -1
      grammar = atom.grammars.grammarForScopeName(contains[namePart])
      return unless grammar?
      editor.setGrammar(grammar)
      return

This automatically sets the grammar to source.log, if the file doesn't have a grammar set and if the name contains the word "log".

Could something similar (with cleaner code) be added by default to the package?

tujula commented 9 years ago

@kankaristo can you come to our company slack.

mrodalgaard commented 9 years ago

Glad you like the package.

I like your snippet, but I don't think coffeescript in language packages are a very good idea. And I see nobody else during something similar.

I currently look for filetypes txt and log, and try to do a first line match for a time stamp. Maybe this can be improved to suit your needs instead?

kankaristo commented 9 years ago

True, I couldn't find any other language-* package that has a lib/ directory. I kind of wish Atom had support for something like this, since some log files just end in "log", not necessarily ".log" (e.g. /var/log/syslog). Sometimes the file name doesn't have "log" in it, but it's inside /var/log/ or a logs/ directory, etc.

I guess I'll close this issue and stick to having the snippet in init.coffee. :)

Unrelated to this issue, but have you considered adding .out (and maybe .output) to the list of file types? It's less common than .log, but Atom actually creates a log file in ~/.atom/nohup.out on Linux (at least when run from the terminal).

mrodalgaard commented 9 years ago

I added out, output and syslog to file formats. And added a link to your init.coffee snippet to readme for others to find.