Skip to content

Commit

Permalink
Add timestamps for logs into the functional tests reports (elastic#24509
Browse files Browse the repository at this point in the history
)

* feat(NA): first version to add logs to the functional test runner when running on dev.

* fix(NA): change timestamp position.

* test(NA): add unit test for tooling log text writer with timestamp.

* test(NA): update test and snapshots

* fix(NA): only apply timestamp logs for the jenkins test reports.

* test(NA): update jest snapshots correctly.

* fix(NA): only add timestamp for the test results cached chunk logs.

* fix(NA): rollback bad changes.

* refact(NA): change comments on code to be more clear with the purpose.

* refact(NA): rename chunk to line.

* feat(NA): log the relative time info since the start time of the task instead of the absolute time.
  • Loading branch information
mistic committed Nov 6, 2018
1 parent 81bfe01 commit 5d7cd17
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions src/functional_test_runner/lib/mocha/reporter/reporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { format } from 'util';

import Mocha from 'mocha';
import { ToolingLogTextWriter } from '@kbn/dev-utils';
import moment from 'moment';

import { setupJUnitReportGeneration } from '../../../../dev';
import * as colors from './colors';
Expand All @@ -33,6 +34,7 @@ export function MochaReporterProvider({ getService }) {
const log = getService('log');
const config = getService('config');
let originalLogWriters;
let reporterCaptureStartTime;

return class MochaReporter extends Mocha.reporters.Base {
constructor(runner, options) {
Expand Down Expand Up @@ -60,7 +62,10 @@ export function MochaReporterProvider({ getService }) {
onStart = () => {
if (config.get('mochaReporter.captureLogOutput')) {
log.warning('debug logs are being captured, only error logs will be written to the console');

reporterCaptureStartTime = moment();
originalLogWriters = log.getWriters();

log.setWriters([
new ToolingLogTextWriter({
level: 'error',
Expand All @@ -69,7 +74,7 @@ export function MochaReporterProvider({ getService }) {
new ToolingLogTextWriter({
level: 'debug',
writeTo: {
write: (chunk) => {
write: (line) => {
// if the current runnable is a beforeEach hook then
// `runner.suite` is set to the suite that defined the
// hook, rather than the suite executing, so instead we
Expand All @@ -80,7 +85,14 @@ export function MochaReporterProvider({ getService }) {
? this.runner.test.parent
: this.runner.suite;

recordLog(currentSuite, chunk);
// We are computing the difference between the time when this
// reporter has started and the time when each line are being
// logged in order to be able to label the test results log lines
// with this relative time information
const diffTimeSinceStart = moment().diff(reporterCaptureStartTime);
const readableDiffTimeSinceStart = `[${moment(diffTimeSinceStart).format('HH:mm:ss')}] `;

recordLog(currentSuite, `${readableDiffTimeSinceStart} ${line}`);
}
}
})
Expand Down

0 comments on commit 5d7cd17

Please sign in to comment.