This is an example of integrating Winston & Morgan Logging with Loopback 4. It uses the express-composition example that mounts the LoopBack 4 REST API on a simple Express application. See here for the express-composition example.
The Express application in this example is based on the Express Generator
You'll need to install the LoopBack 4 CLI toolkit to scaffold your own application:
npm i -g @loopback/cli
You can begin by visiting the official tutorial page for mounting Loopback 4 in Express.
If you'd like to see the final results of this tutorial as an example application, follow these steps:
-
Download/Clone this project.
-
Jump into the directory and then install the required dependencies:
cd loopback4-express-with-logging npm install
-
Finally, start the application!
$ npm start Server is running at http://127.0.0.1:8000
-
Open the Loopback Explorer at http://127.0.0.1:8000/api/explorer/
Feel free to look around in the application's code to get a feel for how it works.
import { winstonLogger } from "./lib/logger";
// log the error.
winstonLogger.error(
`${err.status || 500} - ${err.message} - ${req.originalUrl} - ${
req.method
} - ${req.ip}`
);
You can use the /errorlogger endpoint to log an error from your front end application as well -
curl -X POST "http://127.0.0.1:8000/api/errorlogger" -H "accept: application/json" -H "Content-Type: application/json" -d "{\"message\":\"My awesome error message\"}"
Log files are separated out based on the log levels. The logs
directory is generated at run time.
Application works with Docker. Use the docker-compose
to create your Docker container.
MIT