![Oracle Drags Its Feet in the JavaScript Trademark Dispute](https://cdn.sanity.io/images/cgdhsj6q/production/919c3b22c24f93884c548d60cbb338e819ff2435-1024x1024.webp?w=400&fit=max&auto=format)
Security News
Oracle Drags Its Feet in the JavaScript Trademark Dispute
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
your-logger-lite
Advanced tools
A simple logging package with built-in traceability for managing requests, handlers, and controllers in ExpressJS applications
Your-Logger-Lite is a simple tool that provides your ExpressJS applications with basic logging capabilities and traceability by leveraging middleware.
Your-Logger-Lite is built using typescript and ships with type support. It is configured to allow for easy implementation both with commonJS and ES6.
Uisng yarn:
$ yarn add your-logger-lite
Using npm:
$ npm install your-logger-lite
your-logger-lite depends on an environment variable named LOG_LEVEL
. The concept of log-level will be discussed in further detail a little later. In brief, log-level allows some basic, global, environment-bound control over whether or not certain actions are logged. your-logger-lite's loggerMiddleware
accesses the LOG_LEVEL
by calling process.env.LOG_LEVEL
, as such it is important to configure this variable. The default level is set to 4, so that if you fail to provide this environment variable, the logger will still function and will show all log levels.
Initialize your NodeJS + ExpressJS application as you normally would. Import both loggerMiddleware
and traceMiddleware
from 'your-logger-lite'
. Add these pieces of middleware using app.use
. It is important that you initialize them in this order:
app.use(traceMiddleware)
app.use(loggerMiddleware)
Here is an arbitrary example of a basic express server set-up, shown both using ES6 and commonJS:
// index.js
import express from 'express';
import cors from 'cors;
import { loggerMiddleware, traceMiddleware } from 'your-logger-lite'
// any other imports & configs such as database connections
const app = express()
app.use(express.json());
app.use(cors());
app.use(express.urlencoded({ extended: true }));
app.use(traceMiddleware)
app.use(loggerMiddleware)
// your endpoints
// your app.listen()
// index.js
var express = require('express')
var cors = require('cors')
var logger = require('your-logger-lite')
// any other imports & configs such as database connections
var app = express()
app.use(express.json());
app.use(cors());
app.use(express.urlencoded({ extended: true }));
app.use(logger.traceMiddleware)
app.use(logger.loggerMiddleware)
// your endpoints
// your app.listen()
With this basic setup, all of your routes will already have some level of automatic logging. For example, consider the following arbitrary end-point:
app.get('/health', (req, res) => {
res.send('OK')
});
app.listen(process.env.PORT || 3000, () => {
console.log(`App running on port ${process.env.PORT}`)
});
With the middleware properly initialized, the console should show the following:
App running on port 3000
IP: ::1, HOST: localhost, METHOD: GET
While logging this kind of information when in development on a local server is somewhat contrived, this kind of information can be helpful when the application / API is accessible to multiple clients when in production.
Your-Logger-Lite was designed with the handler-controller paradigm in mind, but it can be injected virtually anywhere. Here is an example of how you might use it to add logging to an arbitrary handler:
// foo.route.js
import { Router } from 'express'
import { handleCreateFoo } from '../handlers/foo.handler'
const fooRouter = Router()
fooRouter.post('/', handleCreateFoo)
---
// foo.handler.js
export const handleCreateFoo = async (req, res) => {
const { context } = req;
const { logger } = context;
const self = handleCreateFoo.name;
logger.start(self);
let newFoo;
try {
newFoo = await createFoo({ req.body, context })
} catch (e) {
logger.error(e, 500, self)
return res.status(500).send(`Could not create newFoo with error: ${e})
}
logger.end(self)
return res.status(200).send(newFoo)
}
---
// foo.contollers.js
// createFoo controller implementation
All that logger.start() does is call logger.info() and logger.time() (while logger.end() calls logger.info() and logger.timeEnd()) which can be called independently, depending on your use-case, giving you further glanularity of control.
FAQs
A simple logging package with built-in traceability for managing requests, handlers, and controllers in ExpressJS applications
We found that your-logger-lite demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
Security News
The Linux Foundation is warning open source developers that compliance with global sanctions is mandatory, highlighting legal risks and restrictions on contributions.
Security News
Maven Central now validates Sigstore signatures, making it easier for developers to verify the provenance of Java packages.