Logging & Debugging
HTTP Request Logging
FoalTS uses morgan to log the HTTP requests. You can specify the output format using the environment variable SETTINGS_LOGGER_FORMAT
or the config/default.json
file:
{
"settings": {
"loggerFormat": "tiny"
}
}
Disabling HTTP Request Logging
In some scenarios and environments, you might want to disable http request logging. You can achieve this by setting the loggerFormat
configuration option to none
.
{
"settings": {
"loggerFormat": "none",
}
}
Disabling Error Logging
In some scenarios, you might want to disable error logging (error stack traces that are displayed when an error is thrown in a controller or hook). You can achieve this by setting the allErrors
configuration option to false.
{
"settings": {
"allErrors": false
}
}
Logging Hook
FoalTS provides a convenient hook for logging debug messages: Log(message: string, options: LogOptions = {})
.
interface LogOptions {
body?: boolean;
params?: boolean;
headers?: string[]|boolean;
query?: boolean;
}
Example:
import { Get, HttpResponseOK, Log } from '@foal/core';
@Log('AppController', {
body: true,
headers: [ 'X-CSRF-Token' ],
params: true,
query: true
})
export class AppController {
@Get()
index() {
return new HttpResponseOK();
}
}
Advanced Logging
If you need advanced logging, you might be interested in using winston, a popular logger in the Node.js ecosystem.
Here's an example on how to use it with Foal:
LoggerService
import * as winston from 'winston';
export class LoggerService {
private logger: any;
constructor() {
this.logger = winston.createLogger({
transports: [
new winston.transports.Console(),
new winston.transports.File({
filename: 'logs.txt'
})
]
});
}
info(msg: string) {
this.logger.info(msg);
}
warn(msg: string) {
this.logger.warn(msg);
}
error(msg: string) {
this.logger.error(msg);
}
}
LogUserId hook
import { Hook } from '@foal/core';
// LoggerService import.
export function LogUserId() {
return Hook((ctx, services) => {
const logger = services.get(LoggerService);
logger.info(`UserId is: ${ctx.user.id}`);
});
}
ProductController
import { Get } from '@foal/core';
// LogUserId import.
export class ProductController {
@Get('/')
@LogUserId()
readProducts() {
...
}
}
AuthController
import { dependency, Post } from '@foal/core';
// LoggerService import.
export class AuthController {
@dependency
logger: LoggerService;
@Post('/signup')
signup() {
...
this.logger.info('Someone signed up!');
}
}