1+ /**
2+ * Summary Logs data from application. Follows a Chain of Responsibility pattern where multiple loggers can be chained.
3+ */
4+ export abstract class AbstractLogger {
5+ LoggerIdentity : String ;
6+ NextLogger : AbstractLogger ;
7+
8+ constructor ( id : string ) {
9+ this . LoggerIdentity = id ;
10+ }
11+
12+ /**
13+ * Summary Logs an event
14+ * @param source Location from where the log is sent
15+ * @param eventName Name of the event that has occurred
16+ * @param properties Properties (KV pair) associated with the event
17+ */
18+ LogEvent ( source : string , eventName : string , properties : any ) {
19+ try {
20+ this . processEvent ( source , eventName , properties ) ;
21+ if ( this . NextLogger !== undefined && this . NextLogger !== null ) {
22+ this . NextLogger . LogEvent ( source , eventName , properties ) ;
23+ }
24+ }
25+ catch {
26+ // DO NOT THROW AN EXCEPTION WHEN LOGGING FAILS
27+ }
28+ }
29+
30+ /**
31+ * Summary Logs an error in the system
32+ * @param source Location where the error has occurred
33+ * @param error Error
34+ * @param properties Custom properties (KV pair)
35+ */
36+ LogException ( source : string , error : Error , properties : any ) {
37+ try {
38+ this . processException ( source , error , properties ) ;
39+ if ( this . NextLogger !== undefined && this . NextLogger !== null ) {
40+ this . NextLogger . LogException ( source , error , properties ) ;
41+ }
42+ }
43+ catch {
44+ // DO NOT THROW AN EXCEPTION WHEN LOGGING FAILS
45+ }
46+ }
47+
48+ /**
49+ * Summary Sets the next logger in the chain. If the next logger is already filled then its chained to the last logger of the chain
50+ * @param nextLogger Next Logger to be set in the chain
51+ */
52+ SetNextLogger ( nextLogger : AbstractLogger ) {
53+ if ( nextLogger === undefined || nextLogger === null )
54+ return ;
55+ if ( ! this . isLoggerLoopCreated ( nextLogger ) ) {
56+ if ( this . NextLogger === undefined || this . NextLogger === null ) {
57+ this . NextLogger = nextLogger ;
58+ } else {
59+ this . NextLogger . SetNextLogger ( nextLogger ) ;
60+ }
61+ }
62+ }
63+
64+ private isLoggerLoopCreated ( nextLogger : AbstractLogger ) {
65+ let tmpLogger = { ...nextLogger } ;
66+ do {
67+ if ( tmpLogger . LoggerIdentity === this . LoggerIdentity )
68+ return true ;
69+ tmpLogger = tmpLogger . NextLogger ;
70+ } while ( tmpLogger !== null || tmpLogger !== undefined )
71+ return false ;
72+ }
73+
74+ abstract processEvent ( source : string , eventName : string , properties : any ) ;
75+ abstract processException ( source , error : Error , properties : any ) ;
76+ }
0 commit comments