W jednej z poprzednich lekcji, napisaliśmy Schematic, który dodaje fonta Lato do index.html, ale jedyną informacją po zakończeniu akcji jest suchy wpis „index.html updated”. Możemy uprzyjemnić korzystanie z Schematics poprzez context.logger:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
export function font(): Rule { return (tree: Tree, context: SchematicContext) => { const fontLink = `<link href="https://fonts.googleapis.com/css?family=Lato" rel="stylesheet">`; appendHtmlElementToHead( tree, getIndexHtmlPath(tree), fontLink, ); context.logger.info( `✅️ Font Lato added to index.html`); }; } |
Jeśli Schematic wykonuje wiele czynności to warto wypunktować wszystkie odpowiednimi logami.
Api loggera:
1 2 3 4 5 6 7 8 9 |
export interface LoggerApi { createChild(name: string): Logger; log(level: LogLevel, message: string, metadata?: JsonObject): void; debug(message: string, metadata?: JsonObject): void; info(message: string, metadata?: JsonObject): void; warn(message: string, metadata?: JsonObject): void; error(message: string, metadata?: JsonObject): void; fatal(message: string, metadata?: JsonObject): void; } |