Last active
March 17, 2020 12:11
-
-
Save lighth7015/af4aaa425075c4fbcd250e9792a5b889 to your computer and use it in GitHub Desktop.
Logging service interface
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const len = <T, TIterable extends Array<T>>(iterable: TIterable): number => iterable.length; | |
const hasMember = <T>(param: T, fieldName: string): boolean => fieldName in param; | |
interface CategoryInfo { | |
id: number; | |
name: string; | |
} | |
interface LogMessage { | |
logText: string; | |
catInfo: CategoryInfo | CategoryInfo[] | string | string[]; | |
logDate: Date; | |
logData: any | any[]; | |
errInfo?: string | object | any[] | null; | |
} | |
interface LogPacket { | |
magicId: number; | |
payload: null | LogMessage | CategoryInfo; | |
} | |
class LoggingService /* extends AbstractCategoryLogger */ { | |
private defMsg: LogMessage = { | |
logText: '', | |
catInfo: [], | |
logData: {}, | |
logDate: new Date | |
}; | |
// This constructor comes from interfacing with the package, "typescript-logging". | |
// It's provided to demonstrate how this class is intended to be used. | |
constructor( /*category: Category, runtimeSettings: RuntimeSettings */) { | |
/* | |
super(category, runtimeSettings); | |
if (len(category.children) > 0) { | |
category.children.forEach( instance => { | |
const packet: LogPacket = this.create(instance); | |
}); | |
} | |
else { | |
const packet: LogPacket = this.create(category); | |
} | |
*/ | |
} | |
private create(payload: CategoryLogMessage | Category): LogPacket { | |
const packet: LogPacket = { magicId: 0x4C4F47474552, payload: null } as LogPacket; | |
if (hasMember(payload, '_message')) { | |
const { message, categories, error, logFormat, date: logDate } = payload as CategoryLogMessage; | |
if (typeof message === typeof {}) { | |
const { msg: logText, data: logData } = message as LogData; | |
packet.payload = { logText, logData } as LogMessage; | |
} | |
// String or null? | |
else { | |
packet.payload = { logText: message } as LogMessage; | |
} | |
packet.payload.catInfo = []; | |
if (len(categories) > 1) { | |
for (const { id, name } of categories) { | |
const category: CategoryInfo = { name, id }; | |
/* | |
ERROR in logging.ts | |
./logging.ts | |
[tsl] ERROR in logging.ts(80,34) | |
TS2345: Argument of type 'CategoryInfo' is not assignable to parameter of type 'CategoryInfo & string'. | |
Type 'CategoryInfo' is not assignable to type 'string'. | |
*/ | |
packet.payload.catInfo.push(category); | |
} | |
} | |
else { | |
const [ category ] = categories; | |
packet.payload.catInfo = category; | |
} | |
if (error != undefined) { | |
console.log({ message, logDate, error, logFormat }); | |
} | |
} | |
else { | |
const { id, name } = payload as Category; | |
packet.payload = { id, name } as CategoryInfo; | |
} | |
return packet; | |
} | |
// This is the only thing you really need to implement. | |
// Here, we'll just write the completed message to the | |
// array. | |
protected doLog(logEvent: CategoryLogMessage): void { | |
this.create(logEvent); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment