-
Notifications
You must be signed in to change notification settings - Fork 25
Winston Logger Adapter #622
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
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
--- | ||
'@graphql-hive/logger-winston': major | ||
--- | ||
|
||
**Winston Adapter** | ||
|
||
Now you can integrate [Winston](https://github.com/winstonjs/winston) into Hive Gateway on Node.js | ||
|
||
```ts | ||
import { defineConfig } from '@graphql-hive/gateway' | ||
import { createLogger, format, transports } from 'winston' | ||
import { createLoggerFromWinston } from '@graphql-hive/winston' | ||
|
||
// Create a Winston logger | ||
const winstonLogger = createLogger({ | ||
level: 'info', | ||
format: format.combine( | ||
format.timestamp(), | ||
format.json() | ||
), | ||
transports: [ | ||
new transports.Console() | ||
] | ||
}) | ||
|
||
export const gatewayConfig = defineConfig({ | ||
// Create an adapter for Winston | ||
logging: createLoggerFromWinston(winstonLogger) | ||
}) | ||
ardatan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
``` | ||
ardatan marked this conversation as resolved.
Show resolved
Hide resolved
ardatan marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
{ | ||
"name": "@graphql-hive/logger-winston", | ||
"version": "0.0.0", | ||
"type": "module", | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/graphql-hive/gateway.git", | ||
"directory": "packages/logger-winston" | ||
}, | ||
"homepage": "https://the-guild.dev/graphql/hive/docs/gateway", | ||
"author": { | ||
"email": "[email protected]", | ||
"name": "The Guild", | ||
"url": "https://the-guild.dev" | ||
}, | ||
"license": "MIT", | ||
"engines": { | ||
"node": ">=18.0.0" | ||
}, | ||
"main": "./dist/index.js", | ||
"exports": { | ||
".": { | ||
"require": { | ||
"types": "./dist/index.d.cts", | ||
"default": "./dist/index.cjs" | ||
}, | ||
"import": { | ||
"types": "./dist/index.d.ts", | ||
"default": "./dist/index.js" | ||
} | ||
}, | ||
"./package.json": "./package.json" | ||
}, | ||
"types": "./dist/index.d.ts", | ||
"files": [ | ||
"dist" | ||
], | ||
"scripts": { | ||
"build": "pkgroll --clean-dist", | ||
"prepack": "yarn build" | ||
}, | ||
"peerDependencies": { | ||
"graphql": "^15.9.0 || ^16.9.0", | ||
"winston": "^3.17.0" | ||
}, | ||
"peerDependenciesMeta": { | ||
"@parcel/watcher": { | ||
"optional": true | ||
} | ||
}, | ||
"dependencies": { | ||
"@graphql-mesh/types": "^0.103.6", | ||
"@whatwg-node/disposablestack": "^0.0.5", | ||
"tslib": "^2.8.1" | ||
}, | ||
"devDependencies": { | ||
"graphql": "16.10.0", | ||
"pkgroll": "2.8.2", | ||
"winston": "^3.17.0" | ||
}, | ||
"sideEffects": false | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
import type { | ||
LazyLoggerMessage, | ||
Logger as MeshLogger, | ||
} from '@graphql-mesh/types'; | ||
import { DisposableSymbols } from '@whatwg-node/disposablestack'; | ||
import type { Logger as WinstonLogger } from 'winston'; | ||
|
||
function prepareArgs(messageArgs: LazyLoggerMessage[]) { | ||
const flattenedMessageArgs = messageArgs | ||
.flat(Infinity) | ||
.flatMap((messageArg) => { | ||
if (typeof messageArg === 'function') { | ||
messageArg = messageArg(); | ||
} | ||
if (messageArg?.toJSON) { | ||
messageArg = messageArg.toJSON(); | ||
} | ||
if (messageArg instanceof AggregateError) { | ||
return messageArg.errors; | ||
} | ||
return messageArg; | ||
}); | ||
let message: string = ''; | ||
const extras: any[] = []; | ||
for (let messageArg of flattenedMessageArgs) { | ||
if (messageArg == null) { | ||
continue; | ||
} | ||
const typeofMessageArg = typeof messageArg; | ||
if ( | ||
typeofMessageArg === 'string' || | ||
typeofMessageArg === 'number' || | ||
typeofMessageArg === 'boolean' | ||
) { | ||
message = message ? message + ', ' + messageArg : messageArg; | ||
} else if (typeofMessageArg === 'object') { | ||
extras.push(messageArg); | ||
} | ||
} | ||
return [message, ...extras] as const; | ||
} | ||
ardatan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
class WinstonLoggerAdapter implements MeshLogger, Disposable { | ||
public name?: string; | ||
constructor( | ||
private winstonLogger: WinstonLogger, | ||
private meta: Record<string, any> = {}, | ||
) { | ||
if (meta['name']) { | ||
this.name = meta['name']; | ||
} | ||
} | ||
log(...args: any[]) { | ||
if (this.winstonLogger.isInfoEnabled()) { | ||
this.winstonLogger.info(...prepareArgs(args)); | ||
} | ||
} | ||
info(...args: any[]) { | ||
if (this.winstonLogger.isInfoEnabled()) { | ||
this.winstonLogger.info(...prepareArgs(args)); | ||
} | ||
} | ||
warn(...args: any[]) { | ||
if (this.winstonLogger.isWarnEnabled()) { | ||
this.winstonLogger.warn(...prepareArgs(args)); | ||
} | ||
} | ||
error(...args: any[]) { | ||
if (this.winstonLogger.isErrorEnabled()) { | ||
this.winstonLogger.error(...prepareArgs(args)); | ||
} | ||
} | ||
debug(...lazyArgs: LazyLoggerMessage[]) { | ||
if (this.winstonLogger.isDebugEnabled()) { | ||
this.winstonLogger.debug(...prepareArgs(lazyArgs)); | ||
} | ||
} | ||
child(nameOrMeta: string | Record<string, string | number>) { | ||
if (typeof nameOrMeta === 'string') { | ||
nameOrMeta = { | ||
name: this.name | ||
? this.name.includes(nameOrMeta) | ||
? this.name | ||
: `${this.name}, ${nameOrMeta}` | ||
: nameOrMeta, | ||
}; | ||
} | ||
return new WinstonLoggerAdapter(this.winstonLogger.child(nameOrMeta), { | ||
...this.meta, | ||
...nameOrMeta, | ||
}); | ||
} | ||
[DisposableSymbols.dispose]() { | ||
return this.winstonLogger.close(); | ||
} | ||
} | ||
|
||
export function createLoggerFromWinston( | ||
winstonLogger: WinstonLogger, | ||
): WinstonLoggerAdapter { | ||
return new WinstonLoggerAdapter(winstonLogger); | ||
} | ||
ardatan marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
import { Writable } from 'node:stream'; | ||
import { describe, expect, it } from 'vitest'; | ||
import * as winston from 'winston'; | ||
import { createLoggerFromWinston } from '../src'; | ||
|
||
describe('Winston', () => { | ||
let log = ''; | ||
let lastCallback = () => {}; | ||
const stream = new Writable({ | ||
write(chunk, _encoding, callback) { | ||
log = chunk.toString('utf-8'); | ||
lastCallback = callback; | ||
}, | ||
}); | ||
const logLevels = ['error', 'warn', 'info', 'debug'] as const; | ||
for (const level of logLevels) { | ||
describe(`Level: ${level}`, () => { | ||
it('basic', () => { | ||
const logger = winston.createLogger({ | ||
level, | ||
format: winston.format.json(), | ||
transports: [ | ||
new winston.transports.Stream({ | ||
stream, | ||
}), | ||
], | ||
}); | ||
using loggerAdapter = createLoggerFromWinston(logger); | ||
const testData = [ | ||
'Hello', | ||
['World'], | ||
{ foo: 'bar' }, | ||
42, | ||
true, | ||
null, | ||
undefined, | ||
() => 'Expensive', | ||
]; | ||
loggerAdapter[level](...testData); | ||
lastCallback(); | ||
const logJson = JSON.parse(log); | ||
expect(logJson).toEqual({ | ||
level, | ||
foo: 'bar', | ||
message: 'Hello, World, 42, true, Expensive', | ||
}); | ||
}); | ||
it('child', () => { | ||
const logger = winston.createLogger({ | ||
level, | ||
format: winston.format.json(), | ||
transports: [ | ||
new winston.transports.Stream({ | ||
stream, | ||
}), | ||
], | ||
}); | ||
const loggerAdapter = createLoggerFromWinston(logger); | ||
const testData = [ | ||
'Hello', | ||
['World'], | ||
{ foo: 'bar' }, | ||
42, | ||
true, | ||
null, | ||
undefined, | ||
() => 'Expensive', | ||
]; | ||
const childLogger = loggerAdapter.child('child'); | ||
childLogger[level](...testData); | ||
lastCallback(); | ||
const logJson = JSON.parse(log); | ||
expect(logJson).toEqual({ | ||
level, | ||
foo: 'bar', | ||
message: 'Hello, World, 42, true, Expensive', | ||
name: 'child', | ||
}); | ||
}); | ||
it('deduplicate names', () => { | ||
const logger = winston.createLogger({ | ||
level, | ||
format: winston.format.json(), | ||
transports: [ | ||
new winston.transports.Stream({ | ||
stream, | ||
}), | ||
], | ||
}); | ||
const loggerAdapter = createLoggerFromWinston(logger); | ||
const testData = [ | ||
'Hello', | ||
['World'], | ||
{ foo: 'bar' }, | ||
42, | ||
true, | ||
null, | ||
undefined, | ||
() => 'Expensive', | ||
]; | ||
const childLogger = loggerAdapter.child('child').child('child'); | ||
childLogger[level](...testData); | ||
lastCallback(); | ||
const logJson = JSON.parse(log); | ||
expect(logJson).toEqual({ | ||
level, | ||
foo: 'bar', | ||
message: 'Hello, World, 42, true, Expensive', | ||
name: 'child', | ||
}); | ||
}); | ||
it('nested', () => { | ||
const logger = winston.createLogger({ | ||
level, | ||
format: winston.format.json(), | ||
transports: [ | ||
new winston.transports.Stream({ | ||
stream, | ||
}), | ||
], | ||
}); | ||
const loggerAdapter = createLoggerFromWinston(logger); | ||
const testData = [ | ||
'Hello', | ||
['World'], | ||
{ foo: 'bar' }, | ||
42, | ||
true, | ||
null, | ||
undefined, | ||
() => 'Expensive', | ||
]; | ||
const childLogger = loggerAdapter.child('child'); | ||
const nestedLogger = childLogger.child('nested'); | ||
nestedLogger[level](...testData); | ||
lastCallback(); | ||
const logJson = JSON.parse(log); | ||
expect(logJson).toEqual({ | ||
level, | ||
foo: 'bar', | ||
message: 'Hello, World, 42, true, Expensive', | ||
name: 'child, nested', | ||
}); | ||
}); | ||
}); | ||
} | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.