karannagupta / loopback4-express-with-logging

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

Home Page:https://karannagupta.com/mounting-loopback-4-rest-api-in-express-with-winston-logging/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

loopback4-express-with-logging

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

Express-Composition Tutorial

You can begin by visiting the official tutorial page for mounting Loopback 4 in Express.

Try out this example

If you'd like to see the final results of this tutorial as an example application, follow these steps:

  1. Download/Clone this project.

  2. Jump into the directory and then install the required dependencies:

    cd loopback4-express-with-logging
    npm install
  3. Finally, start the application!

    $ npm start
    
    Server is running at http://127.0.0.1:8000
  4. 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.

Winston Logging Example

import { winstonLogger } from "./lib/logger";
// log the error.
winstonLogger.error(
  `${err.status || 500} - ${err.message} - ${req.originalUrl} - ${
    req.method
  } - ${req.ip}`
);

Winston Logging Endpoint Example

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

Log files are separated out based on the log levels. The logs directory is generated at run time.

Docker

Application works with Docker. Use the docker-compose to create your Docker container.

Screenshots

1. Log Files

Log Files

2. Error Logger endpoint - /api/errorlogger - POST

errorlogger endpoint

License

MIT

About

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

https://karannagupta.com/mounting-loopback-4-rest-api-in-express-with-winston-logging/

License:Other


Languages

Language:TypeScript 77.8%Language:HTML 17.3%Language:JavaScript 3.9%Language:Dockerfile 1.0%