Skip to content

Commit 4072a94

Browse files
ofrobotsJustinBeckwith
authored andcommitted
refactor!: only support winston3 (#297)
1 parent 7570e5e commit 4072a94

10 files changed

Lines changed: 67 additions & 398 deletions

File tree

handwritten/logging-winston/bin/install-winston2.js

Lines changed: 0 additions & 45 deletions
This file was deleted.

handwritten/logging-winston/package.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
"postcompile": "cpy ./src/types/* ./build/src/types",
4747
"fix": "gts fix && eslint --fix '**/*.js'",
4848
"prepare": "npm run compile",
49-
"pretest": "npm run compile && node ./bin/install-winston2.js",
49+
"pretest": "npm run compile",
5050
"posttest": "npm run check",
5151
"docs-test": "linkinator docs -r --skip www.googleapis.com",
5252
"predocs-test": "npm run docs"
@@ -56,7 +56,6 @@
5656
"google-auth-library": "^3.1.0",
5757
"lodash.mapvalues": "^4.6.0",
5858
"logform": "^2.0.0",
59-
"semver": "^6.0.0",
6059
"winston-transport": "^4.2.0"
6160
},
6261
"devDependencies": {
@@ -91,6 +90,6 @@
9190
"linkinator": "^1.1.2"
9291
},
9392
"peerDependencies": {
94-
"winston": ">=2.x <=3.x"
93+
"winston": "3.x"
9594
}
9695
}

handwritten/logging-winston/src/index.ts

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11

2-
import * as semver from 'semver';
3-
import {LOGGING_TRACE_KEY as COMMON_TRACE_KEY} from './common';
4-
import * as types from './types/core';
2+
import * as TransportStream from 'winston-transport';
53

4+
import {LOGGING_TRACE_KEY as COMMON_TRACE_KEY, LoggingCommon} from './common';
5+
import * as types from './types/core';
66

7-
require('winston');
8-
const winstonVersion = require('winston/package.json').version;
7+
type Callback = (err: Error, apiResponse: {}) => void;
98

109
/**
1110
* This module provides support for streaming your winston logs to
@@ -85,12 +84,29 @@ const winstonVersion = require('winston/package.json').version;
8584
* region_tag:logging_winston_quickstart
8685
* Full quickstart example:
8786
*/
88-
// This is a class.
89-
// tslint:disable-next-line:variable-name
90-
export const LoggingWinston: types.Logger = semver.lt(winstonVersion, '3.0.0') ?
91-
require('./winston2').LoggingWinston :
92-
require('./winston3').LoggingWinston;
93-
// winstons are required instead of imported so they are not executed unless
94-
// they're used.
87+
export class LoggingWinston extends TransportStream {
88+
static readonly LOGGING_TRACE_KEY = COMMON_TRACE_KEY;
89+
90+
common: LoggingCommon;
91+
constructor(options?: types.Options) {
92+
options = options || {};
93+
94+
super({
95+
level: options.level,
96+
});
97+
98+
this.common = new LoggingCommon(options);
99+
}
100+
101+
log({message, level, splat, stack, ...metadata}: types.Winston3LogArg,
102+
callback: Callback) {
103+
// If the whole message is an error we have to manually copy the stack into
104+
// metadata. Errors dont have enumerable properties so they don't
105+
// destructure.
106+
if (stack) metadata.stack = stack;
107+
108+
this.common.log(level, message, metadata || {}, callback);
109+
}
110+
}
95111

96112
export const LOGGING_TRACE_KEY = COMMON_TRACE_KEY;

handwritten/logging-winston/src/types/core.d.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
* limitations under the License.
1515
*/
1616

17-
import * as TransportStream from "winston-transport";
17+
import * as TransportStream from 'winston-transport';
18+
import {GoogleAuth} from 'google-auth-library';
1819

1920

2021
export interface Options {
@@ -168,6 +169,7 @@ interface StackdriverOtherFunctions {
168169
callback?:
169170
(err: Error, apiResponse: {}) => void) => Promise<LogWriteResponse>;
170171
entry: (metadata: {}, data: {}|string) => StackdriverEntry;
172+
logging: { auth: GoogleAuth };
171173
}
172174

173175
export type StackdriverLog = StackdriverLogFunctions&StackdriverOtherFunctions;

handwritten/logging-winston/src/winston2.ts

Lines changed: 0 additions & 64 deletions
This file was deleted.

handwritten/logging-winston/src/winston3.ts

Lines changed: 0 additions & 47 deletions
This file was deleted.

handwritten/logging-winston/system-test/logging-winston.ts

Lines changed: 8 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,10 @@ import {ErrorsApiTransport} from './errors-transport';
2323

2424
const inject = require('require-inject');
2525

26-
const winston3 = require('winston');
27-
const winston2 = require('../../test/winston-2/node_modules/winston');
26+
const winston = require('winston');
2827

2928
const {Logging} = require('@google-cloud/logging');
3029
const logging = new Logging();
31-
const LoggingWinston = require('../src/index').LoggingWinston;
3230

3331
const WRITE_CONSISTENCY_DELAY_MS = 90000;
3432

@@ -84,58 +82,7 @@ describe('LoggingWinston', function() {
8482
args: any[]; level: string; verify: (entry: types.StackdriverEntry) => void;
8583
};
8684

87-
describe('log winston 2', () => {
88-
const testData = commonTestData.concat([
89-
{
90-
args: [new Error('fourth')],
91-
level: 'error',
92-
verify: (entry: types.StackdriverEntry) => {
93-
assert((entry.data as {
94-
message: string
95-
}).message.startsWith('Error: fourth'));
96-
},
97-
},
98-
{
99-
args: ['fifth message', new Error('fifth')],
100-
level: 'error',
101-
verify: (entry: types.StackdriverEntry) => {
102-
assert((entry.data as {
103-
message: string
104-
}).message.startsWith('fifth message Error: fifth'));
105-
},
106-
},
107-
] as typeof commonTestData);
108-
109-
const LOG_NAME = logName('logging_winston_2_system_tests');
110-
const LoggingWinston = inject('../src/index', {
111-
winston: winston2,
112-
'winston/package.json': {version: '2.2.0'}
113-
}).LoggingWinston;
114-
115-
const logger = new winston2.Logger({
116-
transports: [new LoggingWinston({logName: LOG_NAME})],
117-
});
118-
119-
it('should properly write log entries', async () => {
120-
const start = Date.now();
121-
testData.forEach((test) => {
122-
// logger does not have index signature.
123-
// tslint:disable-next-line:no-any
124-
(logger as any)[test.level].apply(logger, test.args);
125-
});
126-
127-
const entries = await pollLogs(
128-
LOG_NAME, start, testData.length, WRITE_CONSISTENCY_DELAY_MS);
129-
assert.strictEqual(entries.length, testData.length);
130-
entries.reverse().forEach((entry, index) => {
131-
const test = testData[index];
132-
test.verify(entry);
133-
});
134-
});
135-
});
136-
137-
138-
describe('log winston 3', () => {
85+
describe('log', () => {
13986
const testData = commonTestData.concat([
14087
{
14188
args: [new Error('fourth')],
@@ -161,13 +108,12 @@ describe('LoggingWinston', function() {
161108
},
162109
] as TestData[]);
163110

164-
const LOG_NAME = logName('logging_winston_3_system_tests');
111+
const LOG_NAME = logName('logging_winston_system_tests');
165112
const LoggingWinston = inject('../src/index', {
166-
winston: winston3,
167-
'winston/package.json': {version: '3.0.0'}
113+
winston,
168114
}).LoggingWinston;
169115

170-
const logger = winston3.createLogger({
116+
const logger = winston.createLogger({
171117
transports: [new LoggingWinston({logName: LOG_NAME})],
172118
});
173119

@@ -194,42 +140,14 @@ describe('LoggingWinston', function() {
194140
const ERROR_REPORTING_POLL_TIMEOUT = WRITE_CONSISTENCY_DELAY_MS;
195141
const errorsTransport = new ErrorsApiTransport();
196142

197-
it('reports errors when logging errors with winston2', async () => {
198-
const start = Date.now();
199-
const service = `logging-winston-system-test-winston2-${UUID}`;
200-
const LoggingWinston = inject('../src/index', {
201-
winston: winston2,
202-
'winston/package.json': {version: '2.2.0'}
203-
}).LoggingWinston;
204-
205-
const logger = new winston2.Logger({
206-
transports: [new LoggingWinston(
207-
{logName: LOG_NAME, serviceContext: {service, version: 'none'}})],
208-
});
209-
210-
const message = `an error at ${Date.now()}`;
211-
212-
logger.error('an error', new Error(message));
213-
214-
const errors = await errorsTransport.pollForNewEvents(
215-
service, start, ERROR_REPORTING_POLL_TIMEOUT);
216-
217-
assert.strictEqual(errors.length, 1);
218-
const errEvent = errors[0];
219-
220-
assert.strictEqual(errEvent.serviceContext.service, service);
221-
assert(errEvent.message.startsWith(`an error Error: ${message}`));
222-
});
223-
224-
it('reports errors when logging errors with winston3', async () => {
143+
it('reports errors', async () => {
225144
const start = Date.now();
226145
const service = `logging-winston-system-test-winston3-${UUID}`;
227146
const LoggingWinston = inject('../src/index', {
228-
winston: winston3,
229-
'winston/package.json': {version: '3.0.0'}
147+
winston,
230148
}).LoggingWinston;
231149

232-
const logger = winston3.createLogger({
150+
const logger = winston.createLogger({
233151
transports: [new LoggingWinston(
234152
{logName: LOG_NAME, serviceContext: {service, version: 'none'}})],
235153
});

0 commit comments

Comments
 (0)