-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLoggerPlugin.ts
91 lines (75 loc) · 2.95 KB
/
LoggerPlugin.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import { DocumentNode, ExecutionArgs, OperationDefinitionNode } from "graphql";
import { SchemaTransform } from "@graphql-tools/utils";
import { GraphQLPlugin, Executable } from ".";
type LogFunction = (...msg: string[]) => void;
interface LeveledLogger {
error?: LogFunction;
info?: LogFunction;
debug?: LogFunction;
}
interface LoggerPluginOptions {
logger?: LogFunction;
leveled?: LeveledLogger;
logQueries?: boolean;
debugLogFullQueries?: boolean;
}
/**
* Logs errors that occur during query execution, including those that
* are filtered by the `graphql-api-koa` errorHandler.
*/
export class LoggerPlugin implements GraphQLPlugin {
constructor(private readonly options: LoggerPluginOptions = { logger: console.log }) {
if (!options.leveled && !options.logger) {
options.logger = console.log;
}
}
directives(): (string | DocumentNode)[] {
return [];
}
transforms(): SchemaTransform[] {
return [];
}
wrapper = (next: Executable): Executable =>
async (args: ExecutionArgs) => {
try {
let requestStart = BigInt(0);
if (this.options.debugLogFullQueries) {
this.debug(args.document.loc?.source?.body || "");
}
if (this.options.logQueries) {
requestStart = process.hrtime.bigint();
}
const result = await next(args);
if (result.errors) {
const messages = result.errors.map((error) => `Error at "${error.path?.join(".")}": ${error.stack || error.message}`);
this.error(...messages);
}
if (this.options.logQueries) {
const duration = Number(process.hrtime.bigint() - requestStart) / 1E6;
const operations = args.document.definitions
.filter((def): def is OperationDefinitionNode => def.kind === "OperationDefinition")
.map((opDef: OperationDefinitionNode) => opDef.operation)
.join(", ");
this.info(`Executed ${operations} with ${result.errors ? "errors" : "success"} in ${duration.toPrecision(4)}ms`);
}
return result;
} catch (error) {
const err = error as Error;
this.error(err.message);
throw error;
}
};
private error(...messages: string[]): void {
const logger = this.options.leveled?.error || this.options.logger;
logger?.(...messages);
}
private info(...messages: string[]): void {
const logger = this.options.leveled?.info || this.options.logger;
logger?.(...messages);
}
private debug(...messages: string[]): void {
const logger = this.options.leveled?.debug || this.options.logger;
logger?.(...messages);
}
}
export default LoggerPlugin;