Skip to content
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
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
public
src
.dockerignore
.gitignore
Dockerfile
Dockerfile.dev
LICENSE
README.md
docker-compose.dev.yml
docker-compose.yml
index.js
package-lock.json
package.json
tsconfig.json

README.md

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

You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.