Skip to content

Commit 328863b

Browse files
sonicoder86ngabor84
andcommitted
fix(private): convert underscore methods to private
EME-5173 Co-authored-by: Gabor Nemeth <[email protected]>
1 parent 252a7e7 commit 328863b

File tree

2 files changed

+54
-54
lines changed

2 files changed

+54
-54
lines changed

src/logger/logger.ts

Lines changed: 40 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -30,20 +30,20 @@ export interface LoggerConfig {
3030
}
3131

3232
export class Logger {
33-
_namespace: string;
34-
_enabled: boolean;
33+
private readonly namespace: string;
34+
private readonly enabled: boolean;
3535

3636
constructor(namespace: string, enabled: boolean) {
37-
this._namespace = namespace;
38-
this._enabled = enabled;
37+
this.namespace = namespace;
38+
this.enabled = enabled;
3939
}
4040

4141
static configure(options: Partial<LoggerConfig>) {
42-
this._validate(options);
42+
this.validate(options);
4343
Object.assign(Logger.config, options);
4444
}
4545

46-
static _validate(options: Partial<LoggerConfig>) {
46+
private static validate(options: Partial<LoggerConfig>) {
4747
Object.keys(options).forEach((key) => {
4848
if (!allowedKeys.includes(key)) {
4949
throw new Error('Only the following keys are allowed: formatter, output');
@@ -58,41 +58,57 @@ export class Logger {
5858
};
5959

6060
isEnabled() {
61-
return this._enabled;
61+
return this.enabled;
6262
}
6363

6464
trace(action: string, data: unknown = {}) {
65-
this._log('trace', action, data);
65+
this.log('trace', action, data);
6666
}
6767

6868
debug(action: string, data: unknown = {}) {
69-
this._log('debug', action, data);
69+
this.log('debug', action, data);
7070
}
7171

7272
info(action: string, data: unknown = {}) {
73-
this._log('info', action, data);
73+
this.log('info', action, data);
7474
}
7575

7676
warn(action: string, data: unknown = {}) {
77-
this._log('warn', action, data);
77+
this.log('warn', action, data);
7878
}
7979

8080
error(action: string, data: unknown = {}) {
81-
this._log('error', action, data);
81+
this.log('error', action, data);
8282
}
8383

8484
fatal(action: string, data: unknown = {}) {
85-
this._log('fatal', action, data);
85+
this.log('fatal', action, data);
8686
}
8787

88-
_log(level: string, action: string, data: unknown) {
89-
if (!this._enabled) {
88+
customError(severity: string, action: string, error: Error, data: unknown = {}) {
89+
this.log(severity, action, Object.assign(this.getErrorDetails(error), data));
90+
}
91+
92+
fromError(action: string, error: unknown, data: unknown = {}) {
93+
this.customError('error', action, error as Error, data);
94+
}
95+
96+
warnFromError(action: string, error: unknown, data: unknown = {}) {
97+
this.customError('warn', action, error as Error, data);
98+
}
99+
100+
timer() {
101+
return new Timer(this);
102+
}
103+
104+
private log(level: string, action: string, data: unknown) {
105+
if (!this.enabled) {
90106
return;
91107
}
92108

93109
let dataToLog = Object.assign(
94110
{
95-
name: this._namespace,
111+
name: this.namespace,
96112
action: action,
97113
level: config.levels[level].number,
98114
time: new Date().toISOString(),
@@ -107,31 +123,15 @@ export class Logger {
107123
Logger.config.output(Logger.config.formatter(dataToLog));
108124
}
109125

110-
customError(severity: string, action: string, error: Error, data: unknown = {}) {
111-
this._log(severity, action, Object.assign(this._getErrorDetails(error), data));
112-
}
113-
114-
fromError(action: string, error: unknown, data: unknown = {}) {
115-
this.customError('error', action, error as Error, data);
116-
}
117-
118-
warnFromError(action: string, error: unknown, data: unknown = {}) {
119-
this.customError('warn', action, error as Error, data);
120-
}
121-
122-
timer() {
123-
return new Timer(this);
124-
}
125-
126-
_shortenStackTrace(stack: string) {
126+
private shortenStackTrace(stack: string) {
127127
if (!stack) {
128128
return;
129129
}
130130

131131
return stack.length > STACK_TRACE_LIMIT ? stack.substring(0, STACK_TRACE_LIMIT) + ' ...' : stack;
132132
}
133133

134-
_shortenData(data: unknown) {
134+
private shortenData(data: unknown) {
135135
if (typeof data === 'undefined') {
136136
return;
137137
}
@@ -141,22 +141,22 @@ export class Logger {
141141
return stringifiedData.length > DATA_LIMIT ? stringifiedData.substring(0, DATA_LIMIT) + ' ...' : stringifiedData;
142142
}
143143

144-
_getErrorDetails(error: Error) {
144+
private getErrorDetails(error: Error) {
145145
if (!(error instanceof Object)) {
146146
return {};
147147
}
148148

149149
const baseDetails = {
150150
error_name: error.name,
151-
error_stack: this._shortenStackTrace(error.stack || ''),
151+
error_stack: this.shortenStackTrace(error.stack || ''),
152152
error_message: error.message,
153-
error_data: this._shortenData((error as ErrorWithData).data),
153+
error_data: this.shortenData((error as ErrorWithData).data),
154154
};
155155

156-
return Object.assign(baseDetails, this._getAxiosErrorDetails(error as AxiosError));
156+
return Object.assign(baseDetails, this.getAxiosErrorDetails(error as AxiosError));
157157
}
158158

159-
_getAxiosErrorDetails(error: AxiosError) {
159+
private getAxiosErrorDetails(error: AxiosError) {
160160
if (!error.isAxiosError) {
161161
return {};
162162
}
@@ -166,7 +166,7 @@ export class Logger {
166166
request_url: error.config.url,
167167
response_status: error.response ? error.response.status : undefined,
168168
response_status_text: error.response ? error.response.statusText : undefined,
169-
response_data: error.response ? this._shortenData(error.response.data) : undefined,
169+
response_data: error.response ? this.shortenData(error.response.data) : undefined,
170170
};
171171
}
172172
}

src/timer/timer.ts

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,49 @@
11
import { Logger } from '../logger/logger';
22

33
export class Timer {
4-
_logger: Logger;
5-
_start: number;
4+
private readonly start: number;
5+
private readonly logger: Logger;
66

77
constructor(logger: Logger) {
8-
this._logger = logger;
9-
this._start = new Date().getTime();
8+
this.logger = logger;
9+
this.start = new Date().getTime();
1010
}
1111

1212
trace(action: string, data: unknown = {}) {
13-
this._logger.trace(action, Object.assign({ duration: this._duration() }, data));
13+
this.logger.trace(action, Object.assign({ duration: this.duration() }, data));
1414
}
1515

1616
debug(action: string, data: unknown = {}) {
17-
this._logger.debug(action, Object.assign({ duration: this._duration() }, data));
17+
this.logger.debug(action, Object.assign({ duration: this.duration() }, data));
1818
}
1919

2020
info(action: string, data: unknown = {}) {
21-
this._logger.info(action, Object.assign({ duration: this._duration() }, data));
21+
this.logger.info(action, Object.assign({ duration: this.duration() }, data));
2222
}
2323

2424
warn(action: string, data: unknown = {}) {
25-
this._logger.warn(action, Object.assign({ duration: this._duration() }, data));
25+
this.logger.warn(action, Object.assign({ duration: this.duration() }, data));
2626
}
2727

2828
error(action: string, data: unknown = {}) {
29-
this._logger.error(action, Object.assign({ duration: this._duration() }, data));
29+
this.logger.error(action, Object.assign({ duration: this.duration() }, data));
3030
}
3131

3232
fatal(action: string, data: unknown = {}) {
33-
this._logger.fatal(action, Object.assign({ duration: this._duration() }, data));
33+
this.logger.fatal(action, Object.assign({ duration: this.duration() }, data));
3434
}
3535

3636
fromError(action: string, error: unknown, data: unknown = {}) {
37-
this._logger.fromError(action, error, Object.assign({ duration: this._duration() }, data));
37+
this.logger.fromError(action, error, Object.assign({ duration: this.duration() }, data));
3838
}
3939

4040
warnFromError(action: string, error: unknown, data: unknown = {}) {
41-
this._logger.warnFromError(action, error, Object.assign({ duration: this._duration() }, data));
41+
this.logger.warnFromError(action, error, Object.assign({ duration: this.duration() }, data));
4242
}
4343

44-
_duration(): number {
44+
private duration(): number {
4545
const end = new Date().getTime();
4646

47-
return end - this._start;
47+
return end - this.start;
4848
}
4949
}

0 commit comments

Comments
 (0)