Browse code

Add VERBOSE_NO_TIMESTAMP flag

As described in the documentation, this flag is intended for the case
where the console output is being captured by a tool that appends its
own timestamps.

In the gate this is the job-output.txt. We want the console output as
people like to watch that scrolling by as part of the live console
log. Although this gets saved to job-output.txt, we still want to
keep logging to the individual log files even though it's technically
a duplicate -- in the multinode case the job-output.txt gets
interleaved by all the running nodes; it's much easier to just look at
the individual log files. Also, people are used to it where it is :)

Change-Id: I3486636f1c76139581f6cd9668426f507b7c621d

Ian Wienand authored on 2018/02/06 08:03:34
Showing 4 changed files
... ...
@@ -66,6 +66,7 @@
66 66
         LOGFILE: /opt/stack/logs/devstacklog.txt
67 67
         LOG_COLOR: false
68 68
         VERBOSE: true
69
+        VERBOSE_NO_TIMESTAMP: true
69 70
         NOVNC_FROM_PACKAGE: true
70 71
         ERROR_ON_CLONE: true
71 72
         # Gate jobs can't deal with nested virt. Disable it.
... ...
@@ -286,6 +286,18 @@ see what is going on. This can be disabled with::
286 286
 
287 287
         LOG_COLOR=False
288 288
 
289
+When using the logfile, by default logs are sent to the console and
290
+the file.  You can set ``VERBOSE`` to ``false`` if you only wish the
291
+logs to be sent to the file (this may avoid having double-logging in
292
+some cases where you are capturing the script output and the log
293
+files).  If ``VERBOSE`` is ``true`` you can additionally set
294
+``VERBOSE_NO_TIMESTAMP`` to avoid timestamps being added to each
295
+output line sent to the console.  This can be useful in some
296
+situations where the console output is being captured by a runner or
297
+framework (e.g. Ansible) that adds its own timestamps.  Note that the
298
+log lines sent to the ``LOGFILE`` will still be prefixed with a
299
+timestamp.
300
+
289 301
 Logging the Service Output
290 302
 ~~~~~~~~~~~~~~~~~~~~~~~~~~
291 303
 
... ...
@@ -405,6 +405,7 @@ is_package_installed python || install_package python
405 405
 
406 406
 # Set up logging level
407 407
 VERBOSE=$(trueorfalse True VERBOSE)
408
+VERBOSE_NO_TIMESTAMP=$(trueorfalse False VERBOSE)
408 409
 
409 410
 # Draw a spinner so the user knows something is happening
410 411
 function spinner {
... ...
@@ -470,8 +471,12 @@ if [[ -n "$LOGFILE" ]]; then
470 470
     # stdout later.
471 471
     exec 3>&1
472 472
     if [[ "$VERBOSE" == "True" ]]; then
473
+        _of_args="-v"
474
+        if [[ "$VERBOSE_NO_TIMESTAMP" == "True" ]]; then
475
+            _of_args="$_of_args --no-timestamp"
476
+        fi
473 477
         # Set fd 1 and 2 to write the log file
474
-        exec 1> >( $TOP_DIR/tools/outfilter.py -v -o "${LOGFILE}" ) 2>&1
478
+        exec 1> >( $TOP_DIR/tools/outfilter.py $_of_args -o "${LOGFILE}" ) 2>&1
475 479
         # Set fd 6 to summary log file
476 480
         exec 6> >( $TOP_DIR/tools/outfilter.py -o "${SUMFILE}" )
477 481
     else
... ...
@@ -36,6 +36,13 @@ def get_options():
36 36
     parser.add_argument('-o', '--outfile',
37 37
                         help='Output file for content',
38 38
                         default=None)
39
+    # NOTE(ianw): This is intended for the case where your stdout is
40
+    # being captured by something like ansible which independently
41
+    # logs timestamps on the lines it receives.  Note that if using a
42
+    # output file, those log lines are still timestamped.
43
+    parser.add_argument('-b', '--no-timestamp', action='store_true',
44
+                        help='Do not prefix stdout with timestamp (bare)',
45
+                        default=False)
39 46
     parser.add_argument('-v', '--verbose', action='store_true',
40 47
                         default=False)
41 48
     return parser.parse_args()
... ...
@@ -61,17 +68,23 @@ def main():
61 61
         if skip_line(line):
62 62
             continue
63 63
 
64
-        # This prevents us from nesting date lines, because
65
-        # we'd like to pull this in directly in Grenade and not double
66
-        # up on DevStack lines
64
+        # This prevents us from nesting date lines, because we'd like
65
+        # to pull this in directly in Grenade and not double up on
66
+        # DevStack lines.
67
+        # NOTE(ianw): we could actually strip the extra ts in "bare"
68
+        # mode (which came after this)? ... as we get more experience
69
+        # with zuulv3 native jobs and ansible capture it may become
70
+        # clearer what to do
67 71
         if HAS_DATE.search(line) is None:
68 72
             now = datetime.datetime.utcnow()
69
-            line = ("%s | %s" % (
73
+            ts_line = ("%s | %s" % (
70 74
                 now.strftime("%Y-%m-%d %H:%M:%S.%f")[:-3],
71 75
                 line))
76
+        else:
77
+            ts_line = line
72 78
 
73 79
         if opts.verbose:
74
-            sys.stdout.write(line)
80
+            sys.stdout.write(line if opts.no_timestamp else ts_line)
75 81
             sys.stdout.flush()
76 82
 
77 83
         if outfile:
... ...
@@ -80,9 +93,9 @@ def main():
80 80
             # opened with the system encoding and made the line into
81 81
             # utf-8, so write the logfile out in utf-8 bytes.
82 82
             if sys.version_info < (3,):
83
-                outfile.write(line)
83
+                outfile.write(ts_line)
84 84
             else:
85
-                outfile.write(line.encode('utf-8'))
85
+                outfile.write(ts_line.encode('utf-8'))
86 86
             outfile.flush()
87 87
 
88 88