Skip to content

Conversation

@tmarshall
Copy link
Contributor

If using a specific logger in python, it is useful to set a custom parser for data printed by python. I've set this as a parser option, which is optional, outside of the normal mode. This means you can leave mode as json, for example, and send json to the python script. Then, when receiving lines from the script, it will first check for parser and then fall back to modes.

For example, if you have a script that outputs something like this:
2015-05-13 13:32:52,812 INFO dosomething:12 root.main() - xyz abc
You can set a parser:

var options = {
  mode: 'json',
  parser: function(line) {
    var expr = /^([\d-]+ [\d:]+),\d* (\w+) (\w+):(\d+) ([\w\.\(\)]+) - (.*)$/;
    var results = expr.exec(line);

    if (!results) {
      throw 'Failed to parse python output';
    }

    return {
      time: new Date(results[1]),
      type: results[2].toLowerCase(),
      module: results[3],
      lineno: +results[4],
      func: results[5],
      content: results[6]
    };
  }
};

This will return

{ time: Wed May 13 2015 13:32:52 GMT-0700 (PDT),
  type: 'info',
  module: 'dosomething',
  lineno: 12,
  func: 'root.main()',
  content: 'xyz abc' }

If a line is invalid it will throw, and python-shell will return a proper error.

@extrabacon
Copy link
Owner

Parsing with a function sure is useful. I would go further and eliminate the mode parameter in order to use functions only. I would still use strings as shortcuts for "text" and "json", but use the new function-based parser parameter instead.

I will make some changes in a new branch in order to test this.

@tmarshall
Copy link
Contributor Author

👍

extrabacon added a commit that referenced this pull request May 14, 2015
new options for formatting and parsing messages with custom functions,
inspired from pr #10.
@extrabacon
Copy link
Owner

Take a look at the changes in https://github.com/extrabacon/python-shell/tree/parsers.

@tmarshall
Copy link
Contributor Author

ah I like the formatter as well. lgtm

@extrabacon extrabacon merged commit 8da89c9 into extrabacon:master May 14, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants