Simple class for writing to the console with TypeScript / SPFx projects
- Log levels: log, info, warn, error
- Support for multiple arguments
- Stringify non-string argument types
- Displays application name, class name, function name and specified arguments
import Log from "./Log"
// the name of your application, used across modules/classes
const applicationName: string = "SomeApplication";
export default class SomeClass {
// create an instance of the Log class, with a reference to the current class
private log: Log = new Log(applicationName, this, "SomeClass");
constructor() {
// pass the calling method (in this case the constructor) as the first argument
// and the data you want to log as the subsequent arguments
this.log.info(this.constructor, "Testing log");
// [SomeApplication][SomeClass][constructor]: Testing log
this.log.info(this.constructor, { guacamole: ["avocado", "chilli", "lime", "salt", "pepper"] });
// [SomeApplication][SomeClass][constructor]: {"guacamole":["avocado","chilli","lime","salt","pepper"]}
this.log.info(this.constructor, 42, "orange");
// [SomeApplication][SomeClass][constructor]: 42, orange
// sometimes just logging method invocation is useful
this.log.info(this.constructor);
// [SomeApplication][SomeClass][constructor]
// also supported, with the same syntax:
// - this.log.log
// - this.log.warn
// - this.log.error
}
}