Logging¶
Colyseus allows you to import a logger
variable, which is customizable via its
initial settings.
import { logger } from "@colyseus/core";
// ...
logger.debug("this is debug");
logger.log("this is a log");
logger.info("this is information");
logger.warn("this is a warning");
logger.error("this is an error");
By default, the logger
instance is just an alias to console
. You may want to
use a different logger than console
for a number of different reasons, such
as:
- External logging storage
- Configure different logging levels
- Customize logging output
- (...) more!
Customizing the logger
Chosing your preferred logger¶
The Node.js ecosystem has a few different logging solutions, each of them has its own advantages and disadvantages, such as:
Using pino
as your logger¶
Install the pino
module:
Configure it in your application:
See full documentation for pino
options.
Using winston
as your logger¶
Install the winston
module:
Configure it in your application:
import config from "@colyseus/tools";
import winston from "winston";
export default config({
// ...
options: {
logger: winston.createLogger({
level: 'info',
format: winston.format.json(),
defaultMeta: { service: 'user-service' },
transports: [
//
// - Write all logs with importance level of `error` or less to `error.log`
// - Write all logs with importance level of `info` or less to `combined.log`
//
new winston.transports.File({ filename: 'error.log', level: 'error' }),
new winston.transports.File({ filename: 'combined.log' }),
],
})
}
// ...
});
import { Server } from "@colyseus/core";
import winston from "winston";
const gameServer = new Server({
logger: winston.createLogger({
level: 'info',
format: winston.format.json(),
defaultMeta: { service: 'user-service' },
transports: [
//
// - Write all logs with importance level of `error` or less to `error.log`
// - Write all logs with importance level of `info` or less to `combined.log`
//
new winston.transports.File({ filename: 'error.log', level: 'error' }),
new winston.transports.File({ filename: 'combined.log' }),
],
})
})