log-2-route
v1.11.4
Published
Logger for NextJS that uses the app router to enable logging from anywhere in your app and beyond.
Downloads
1,040
Maintainers
Readme
About The Project
I was looking for an easy-to-use file logger for my Next.js apps, suitable for both development and production, without the need to deploy to Vercel. I tried several popular solutions, but none of them worked well for my use cases, so of course I had to roll my own.
Getting Started
Prerequisites
Installation
Install
npm i log-2-route
Configure
node ./node_modules/log-2-route/install/config
The config script will perform a few tasks
Configuration
{
"logFile": {
"format": "ndjson",
"enabled": true,
"fileName": "app.log",
"location": "./",
"timeType": "epoch",
"colorizeStyledLog": false
},
"console": {
"format": "styled",
"enabled": true
"timeType": "epoch",
"colorizeStyledLog": true
}
}
Logfile Options
Console Options
Usage
Importing
import { logger } from 'log-2-route';
Logging
logger.info('messsage');
logger.error('message');
logger.success('message');
logger.debug('message');
logger.warn('message');
Example
try {
// codes...
}
catch(e) {
logger.error((e instanceOf Error).message);
logger.debug((e instanceOf Error).stack);
}
*logger.debug() will only output to the logfile.
logger.success('User has done a thing!');
Logger can take multiple arguments and argument types
logger.info('User logged in', { level: 1 }, 12345);
You must wrap the logger in a useEffect hook when using client side
useEffect(()=> logger.info("Logged."), [])
And if set to styled will log
[7378237287] INFO - User logged in level: 1
Output
[12/23/2024, 7:40:59 AM] INFO - User Rudy Schneider logged in
{"type":"info","time":{"epoch":1734957920354},"data":{"message":"User Rudy Schneider logged in"}}
License
Distributed under the MIT. See LICENSE.txt
for more information.
Contact
Nathan Schneider - nlschneider@gmail.com
GitHub: [https://github.com/nathanlschneider/log-2-route] NPM: [https://www.npmjs.com/package/log-2-route]