Skip to content
This repository was archived by the owner on Oct 16, 2024. It is now read-only.

Commit dcf742f

Browse files
committed
added log code manager to react package
1 parent 5ea27e2 commit dcf742f

File tree

5 files changed

+85
-49
lines changed

5 files changed

+85
-49
lines changed

packages/core/src/logCodeManager.ts

Lines changed: 45 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ const logCodeTypes = {
2323
// ---
2424
// 00:00:|00| third digits are based on the Log Message (ascending counted)
2525

26-
const niceLogCodeMessages = {
26+
const logCodeMessages = {
2727
// Agile
2828
'10:00:00': 'Created new AgileInstance.',
2929
'10:02:00':
@@ -165,13 +165,6 @@ const niceLogCodeMessages = {
165165
'00:03:01': "'${0}' has to be of the type ${1}!",
166166
};
167167

168-
// Note: Not outsource the 'production' env check,
169-
// because then webpack can't treeshake based on the current env
170-
const logCodeMessages: typeof niceLogCodeMessages =
171-
typeof process === 'object' && process.env.NODE_ENV !== 'production'
172-
? niceLogCodeMessages
173-
: ({} as any);
174-
175168
/**
176169
* Returns the log message according to the specified log code.
177170
*
@@ -257,20 +250,22 @@ function logIfTags<T extends LogCodesArrayType<typeof logCodeMessages>>(
257250
// Handle logging with Logger
258251
logger.if.tag(tags)[logType](getLog(logCode, replacers), ...data);
259252
}
260-
/**
261-
* The Log Code Manager keeps track
262-
* and manages all important Logs of AgileTs.
263-
*
264-
* @internal
265-
*/
266-
let tempLogCodeManager: {
267-
getLog: typeof getLog;
268-
log: typeof log;
269-
logCodeLogTypes: typeof logCodeTypes;
270-
logCodeMessages: typeof logCodeMessages;
271-
getLogger: () => any;
272-
logIfTags: typeof logIfTags;
273-
};
253+
254+
export function assignAdditionalLogs<
255+
NewLogCodeMessages,
256+
OldLogCodeMessages = typeof logCodeMessages
257+
>(
258+
additionalLogs: { [key: string]: string },
259+
logCodeManager: LogCodeManagerInterface<OldLogCodeMessages>
260+
): LogCodeManagerInterface<NewLogCodeMessages> {
261+
logCodeManager.logCodeMessages = {
262+
...LogCodeManager.logCodeMessages,
263+
...additionalLogs,
264+
} as any;
265+
return logCodeManager as any;
266+
}
267+
268+
let tempLogCodeManager: LogCodeManagerInterface<typeof logCodeMessages>;
274269
if (typeof process === 'object' && process.env.NODE_ENV !== 'production') {
275270
let loggerPackage: any = null;
276271
try {
@@ -284,11 +279,7 @@ if (typeof process === 'object' && process.env.NODE_ENV !== 'production') {
284279
log,
285280
logCodeLogTypes: logCodeTypes,
286281
logCodeMessages: logCodeMessages,
287-
// Not doing 'logger: loggerPackage?.sharedAgileLogger'
288-
// because only by calling a function (now 'getLogger()') the 'sharedLogger' is refetched
289-
getLogger: () => {
290-
return loggerPackage?.sharedAgileLogger ?? null;
291-
},
282+
getLogger: loggerPackage.getLogger,
292283
logIfTags,
293284
};
294285
} else {
@@ -297,20 +288,43 @@ if (typeof process === 'object' && process.env.NODE_ENV !== 'production') {
297288
getLog: (logCode, replacers) => logCode,
298289
log,
299290
logCodeLogTypes: logCodeTypes,
300-
logCodeMessages: logCodeMessages,
301-
// Not doing 'logger: loggerPackage?.sharedAgileLogger'
302-
// because only by calling a function (now 'getLogger()') the 'sharedLogger' is refetched
291+
logCodeMessages: {} as any,
303292
getLogger: () => {
304293
return null;
305294
},
306295
logIfTags: (tags, logCode, replacers) => {
307-
/* empty */
296+
/* empty because logs with tags can't be that important */
308297
},
309298
};
310299
}
300+
301+
/**
302+
* The Log Code Manager keeps track
303+
* and manages all important Logs for the '@agile-ts/core' package.
304+
*
305+
* @internal
306+
*/
311307
export const LogCodeManager = tempLogCodeManager;
312308

313309
export type LogCodesArrayType<T> = {
314310
[K in keyof T]: T[K] extends string ? K : never;
315311
}[keyof T] &
316312
string;
313+
314+
export interface LogCodeManagerInterface<T = typeof logCodeMessages> {
315+
getLog: (logCode: LogCodesArrayType<T>, replacers?: any[]) => string;
316+
log: (
317+
logCode: LogCodesArrayType<T>,
318+
replacers?: any[],
319+
...data: any[]
320+
) => void;
321+
logCodeLogTypes: typeof logCodeTypes;
322+
logCodeMessages: T;
323+
getLogger: () => any;
324+
logIfTags: (
325+
tags: string[],
326+
logCode: LogCodesArrayType<T>,
327+
replacers?: any[],
328+
...data: any[]
329+
) => void;
330+
}

packages/logger/src/index.ts

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import { CreateLoggerConfigInterface, Logger } from './logger';
22
import { defineConfig } from '@agile-ts/utils';
33

4+
export * from './logger';
5+
export default Logger;
6+
47
const defaultLogConfig = {
58
prefix: 'Agile',
69
active: true,
@@ -9,25 +12,24 @@ const defaultLogConfig = {
912
allowedTags: ['runtime', 'storage', 'subscription', 'multieditor'],
1013
};
1114

12-
/**
13-
* Shared Agile Logger.
14-
*/
1515
let sharedAgileLogger = new Logger(defaultLogConfig);
1616

1717
/**
1818
* Assigns the specified configuration object to the shared Agile Logger.
1919
*
2020
* @param config - Configuration object
2121
*/
22-
// https://stackoverflow.com/questions/32558514/javascript-es6-export-const-vs-export-let
23-
function assignSharedAgileLoggerConfig(
22+
export function assignSharedAgileLoggerConfig(
2423
config: CreateLoggerConfigInterface = {}
2524
): Logger {
2625
config = defineConfig(config, defaultLogConfig);
2726
sharedAgileLogger = new Logger(config);
2827
return sharedAgileLogger;
2928
}
3029

31-
export { sharedAgileLogger, assignSharedAgileLoggerConfig };
32-
export * from './logger';
33-
export default Logger;
30+
/**
31+
* Returns the shared Agile Logger.
32+
*/
33+
export function getLogger(): Logger {
34+
return sharedAgileLogger;
35+
}

packages/react/src/hooks/useBaseAgile.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@ import Agile, {
33
Collection,
44
ComponentIdType,
55
getAgileInstance,
6-
LogCodeManager,
76
Observer,
87
State,
98
SubscriptionContainerKeyType,
109
RegisterSubscriptionConfigInterface,
1110
} from '@agile-ts/core';
1211
import { useIsomorphicLayoutEffect } from './useIsomorphicLayoutEffect';
12+
import { LogCodeManager } from '../logCodeManager';
1313

1414
/**
1515
* An internal used React Hook
@@ -46,10 +46,7 @@ export const useBaseAgile = (
4646
// Try to extract Agile Instance from the specified Instance/s
4747
if (agileInstance == null) agileInstance = getAgileInstance(observers[0]);
4848
if (agileInstance == null || agileInstance.subController == null) {
49-
LogCodeManager.getLogger()?.error(
50-
'Failed to subscribe Component with deps because of missing valid Agile Instance.',
51-
deps
52-
);
49+
LogCodeManager.log('30:03:00', deps);
5350
return;
5451
}
5552

packages/react/src/hooks/useProxy.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {
1515
AgileOutputHookArrayType,
1616
AgileOutputHookType,
1717
} from './useAgile';
18+
import { LogCodeManager } from '../logCodeManager';
1819

1920
// TODO https://stackoverflow.com/questions/68148235/require-module-inside-a-function-doesnt-work
2021
let proxyPackage: any = null;
@@ -100,11 +101,7 @@ export function useProxy<
100101
proxyTreeWeakMap.set(dep, proxyTree);
101102
return proxyTree.proxy;
102103
} else {
103-
// TODO add LogCode Manager logcode
104-
console.error(
105-
'In order to use the Agile proxy functionality, ' +
106-
`the installation of an additional package called '@agile-ts/proxytree' is required!`
107-
);
104+
LogCodeManager.log('31:03:00');
108105
}
109106
}
110107

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import {
2+
LogCodeManager as CoreLogCodeManager,
3+
assignAdditionalLogs,
4+
} from '@agile-ts/core';
5+
6+
const additionalLogs = {
7+
'30:03:00':
8+
'Failed to subscribe Component with deps because of missing valid Agile Instance.',
9+
'31:03:00':
10+
"In order to use the Agile proxy functionality, the installation of an additional package called '@agile-ts/proxytree' is required!",
11+
};
12+
13+
/**
14+
* The Log Code Manager keeps track
15+
* and manages all important Logs for the '@agile-ts/react' package.
16+
*
17+
* @internal
18+
*/
19+
export const LogCodeManager =
20+
typeof process === 'object' && process.env.NODE_ENV !== 'production'
21+
? assignAdditionalLogs<
22+
typeof CoreLogCodeManager.logCodeMessages & typeof additionalLogs
23+
>(additionalLogs, CoreLogCodeManager)
24+
: assignAdditionalLogs<
25+
typeof CoreLogCodeManager.logCodeMessages & typeof additionalLogs
26+
>({}, CoreLogCodeManager);

0 commit comments

Comments
 (0)