Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add filtering based on time #46

Open
bcantrill opened this issue Oct 19, 2012 · 3 comments
Open

add filtering based on time #46

bcantrill opened this issue Oct 19, 2012 · 3 comments

Comments

@bcantrill
Copy link
Contributor

When looking at many logs at a go, it's often very convenient to filter based on time. A prototype fix, surely incomplete:

diff --git a/bin/bunyan b/bin/bunyan
index 80bd5f0..959518b 100755
--- a/bin/bunyan
+++ b/bin/bunyan
@@ -149,6 +149,15 @@ function printHelp() {
   console.log("                use the numeric values for filtering by level.")
   console.log("  --strict      Suppress all but legal Bunyan JSON log lines. By
   console.log("                non-JSON, and non-Bunyan lines are passed throug
+  console.log("  -t, --time TIME");
+  console.log("                Only show messages more recent than the specifie
+  console.log("                expressed as seconds in the past.  The time may"
+  console.log("                have an optional 'm', 'd' or 'w' suffix to denot
+  console.log("                minutes, days and weeks, respectively.  For exam
+  console.log("                `-t 5m` would emit all Bunyan log lines from wit
+  console.log("                the past five minutes.  (Note that non-Bunyan lo
+  console.log("                lines are passed through the time filter; see --
+  console.log("                above.)");
   console.log("");
   console.log("Output options:");
   console.log("  --color       Colorize output. Defaults to try if output");
@@ -213,6 +222,16 @@ function filterRecord(rec, opts)
     return false;
   }

+  if (opts.time) {
+    if (!rec.time)
+      return false;
+
+    var t = new Date(rec.time);
+
+    if (!isNaN(t.valueOf()) && t.valueOf() < opts.time)
+      return false;
+  }
+
   if (opts.conditions) {
     for (var i = 0; i < opts.conditions.length; i++) {
       var pass = opts.conditions[i].runInNewContext(rec);
@@ -303,7 +322,8 @@ function parseArgv(argv) {
     jsonIndent: 2,
     level: null,
     conditions: null,
-    strict: false
+    strict: false,
+    time: 0
   };

   // Turn '-iH' into '-i -H', except for argument-accepting options.
@@ -390,6 +410,21 @@ function parseArgv(argv) {
         }
         parsed.level = level;
         break;
+      case "-t":
+      case "--time":
+        var timeArg = args.shift();
+        var multiplier = { s: 1, m: 60, h: 3600, d: 86400, w: 604800 };
+        var m = 1, t;
+
+        if ((m = multiplier[timeArg[timeArg.length - 1]]) !== undefined)
+            timeArg = timeArg.substr(0, timeArg.length - 1);
+
+        if ((t = parseInt(timeArg, 10)) + '' != timeArg || isNaN(t) || t < 0)
+          throw new Error("invalid time value: '"+timeArg+"'");
+
+        parsed.time = new Date().valueOf() - (t * m * 1000);
+        break;
+
       case "-c":
       case "--condition":
         var condition = args.shift();
@trentm
Copy link
Owner

trentm commented Oct 19, 2012

Heh, I had these (in separate places) in my notes:

bunyan -s|--start 5m     # start 5 min ago
bunayn -s time=5m

The idea with the latter one is a more generic "start" option that would potentially eventually allow something like this:

bunyan -s req_id=$uuid

That would scan back through the log file(s) looking for the first record with that req_id field (with some max time/num-records/size back as a guard).

Anyway, that is vapourware, so ignoring.

your proposal

bunyan -t 5s
bunayn -t 5     # presumption is 5 *seconds*

What about this to get the last N lines:

bunyan -t 5l     # that's a lowercase "L"; last 5 *lines*

RobG recently added napictl lastlog to get the last N logs, so there is a valid use case there I think. And if we did add support for this, what about (a) the mnemonic is not "time", but "tail", and (b) the default would be lines. The "l" is weird as a suffix, so I'd just drop that.

bunyan --tail     # last 10 lines (where by "lines" I mean log records)
bunyan -t          # ditto
bunyan -t 5       # last 5 lines
bunyan -t 5s     # last 5 seconds
bunyan -t 5m    # last 5 minutes

Thoughts?

Also, your patch was clipped at 80 columns, cutting off some of your usage text. Can you resubmit that?

@chakrit
Copy link
Contributor

chakrit commented Oct 24, 2013

FWIW I made this little utility for myself: https://github.com/chakrit/bunyan-reltime

@trentm
Copy link
Owner

trentm commented Oct 24, 2013

@chakrit Nice! Thanks for the note.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants