Skip to content

Commit 2542480

Browse files
committed
move logger to ts
1 parent 1bdd193 commit 2542480

File tree

4 files changed

+160
-121
lines changed

4 files changed

+160
-121
lines changed

packages/cubejs-server-core/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ const { CubejsServerCore } = require('./dist/src/core/server');
33

44
/**
55
* After 5 years working with TypeScript, now I know
6-
* that commonjs and nodejs require is not compatibility with using export default
6+
* that commonjs and Node.js require is not compatible with using export default
77
*/
88
const toExport = CubejsServerCore;
99

packages/cubejs-server-core/src/core/logger.js

Lines changed: 0 additions & 118 deletions
This file was deleted.
Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
import SqlString from 'sqlstring';
2+
import R from 'ramda';
3+
4+
export type LogLevel = 'trace' | 'info' | 'warn' | 'error';
5+
6+
interface BaseLogMessage {
7+
requestId?: string;
8+
duration?: number;
9+
query?: string | Record<string, any>;
10+
values?: any[];
11+
allSqlLines?: boolean;
12+
showRestParams?: boolean;
13+
error?: Error | string;
14+
warning?: string;
15+
[key: string]: any;
16+
}
17+
18+
type Color = '31' | '32' | '33';
19+
20+
const colors: Record<'red' | 'green' | 'yellow', Color> = {
21+
red: '31', // ERROR
22+
green: '32', // INFO
23+
yellow: '33', // WARNING
24+
};
25+
26+
const withColor = (str: string, color: Color = colors.green): string => `\u001b[${color}m${str}\u001b[0m`;
27+
28+
interface FormatOptions {
29+
requestId?: string;
30+
duration?: number;
31+
query?: string | Record<string, any>;
32+
values?: any[];
33+
allSqlLines?: boolean;
34+
showRestParams?: boolean;
35+
[key: string]: any;
36+
}
37+
38+
const format = ({ requestId, duration, allSqlLines, query, values, showRestParams, ...json }: FormatOptions): string => {
39+
const restParams = JSON.stringify(json, null, 2);
40+
const durationStr = duration ? `(${duration}ms)` : '';
41+
const prefix = `${requestId || ''} ${durationStr}`.trim();
42+
43+
if (query && values) {
44+
const queryMaxLines = 50;
45+
let queryStr = typeof query === 'string' ? query : JSON.stringify(query);
46+
queryStr = queryStr.replaceAll(/\$(\d+)/g, '?');
47+
let formatted = SqlString.format(queryStr, values).split('\n');
48+
49+
if (formatted.length > queryMaxLines && !allSqlLines) {
50+
formatted = R.take(queryMaxLines / 2, formatted)
51+
.concat(['.....', '.....', '.....'])
52+
.concat(R.takeLast(queryMaxLines / 2, formatted));
53+
}
54+
55+
return `${prefix}\n--\n ${formatted.join('\n')}\n--${showRestParams ? `\n${restParams}` : ''}`;
56+
} else if (query) {
57+
return `${prefix}\n--\n${JSON.stringify(query, null, 2)}\n--${showRestParams ? `\n${restParams}` : ''}`;
58+
}
59+
60+
return `${prefix}${showRestParams ? `\n${restParams}` : ''}`;
61+
};
62+
63+
export const devLogger = (level?: LogLevel) => (type: string, { error, warning, ...message }: BaseLogMessage): void => {
64+
const logWarning = () => console.log(
65+
`${withColor(type, colors.yellow)}: ${format({ ...message, allSqlLines: true, showRestParams: true })} \n${withColor(warning || '', colors.yellow)}`
66+
);
67+
68+
const logError = () => console.log(
69+
`${withColor(type, colors.red)}: ${format({ ...message, allSqlLines: true, showRestParams: true })} \n${error}`
70+
);
71+
72+
const logDetails = (showRestParams?: boolean) => console.log(
73+
`${withColor(type)}: ${format({ ...message, showRestParams })}`
74+
);
75+
76+
if (error) {
77+
logError();
78+
return;
79+
}
80+
81+
// eslint-disable-next-line default-case
82+
switch ((level || 'info').toLowerCase()) {
83+
case 'trace': {
84+
if (!error && !warning) {
85+
logDetails(true);
86+
}
87+
break;
88+
}
89+
case 'info': {
90+
if (!error && !warning && [
91+
'Executing SQL',
92+
'Streaming SQL',
93+
'Executing Load Pre Aggregation SQL',
94+
'Load Request Success',
95+
'Performing query',
96+
'Performing query completed',
97+
'Streaming successfully completed',
98+
].includes(type)) {
99+
logDetails();
100+
}
101+
break;
102+
}
103+
case 'warn': {
104+
if (!error && warning) {
105+
logWarning();
106+
}
107+
break;
108+
}
109+
case 'error': {
110+
if (error) {
111+
logError();
112+
}
113+
break;
114+
}
115+
}
116+
};
117+
118+
interface ProdLogParams {
119+
error?: Error | string;
120+
warning?: string;
121+
[key: string]: any;
122+
}
123+
124+
export const prodLogger = (level?: LogLevel) => (msg: string, params: ProdLogParams): void => {
125+
const { error, warning } = params;
126+
127+
const logMessage = () => console.log(JSON.stringify({ message: msg, ...params }));
128+
129+
// eslint-disable-next-line default-case
130+
switch ((level || 'warn').toLowerCase()) {
131+
case 'trace': {
132+
if (!error && !warning) {
133+
logMessage();
134+
}
135+
break;
136+
}
137+
case 'info':
138+
if ([
139+
'REST API Request',
140+
].includes(msg)) {
141+
logMessage();
142+
}
143+
break;
144+
case 'warn': {
145+
if (!error && warning) {
146+
logMessage();
147+
}
148+
break;
149+
}
150+
case 'error': {
151+
if (error) {
152+
logMessage();
153+
}
154+
break;
155+
}
156+
}
157+
};

packages/cubejs-server-core/src/core/server.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -183,8 +183,8 @@ export class CubejsServerCore {
183183

184184
this.logger = opts.logger || (
185185
process.env.NODE_ENV !== 'production'
186-
? devLogger(process.env.CUBEJS_LOG_LEVEL)
187-
: prodLogger(process.env.CUBEJS_LOG_LEVEL)
186+
? devLogger(process.env.CUBEJS_LOG_LEVEL as any)
187+
: prodLogger(process.env.CUBEJS_LOG_LEVEL as any)
188188
);
189189

190190
this.optsHandler = new OptsHandler(this, opts, systemOptions);

0 commit comments

Comments
 (0)