@tent-official/logs-guard
v1.3.0
Published
A utility to suppress console logs in production environment
Maintainers
Readme
@tent-official/logs-guard
A utility to suppress console logs in production environment.
Installation
npm install @tent-official/logs-guard
# or
yarn add @tent-official/logs-guardUsage
import { logsGuard, logsRelease, logsWarning } from '@tent-official/logs-guard'
// Call at the start of your application (e.g., main.ts)
// This will suppress all specified logs in production
logsGuard()
// To allow a limited number of log lines
logsRelease(1) // Allow 1 line (default)
logsRelease(3) // Allow up to 3 lines (max: 5)
logsRelease(0) // Suppress all logs
// Show styled warning message (Windows only)
logsWarning({
title: {
text: 'Warning!',
style: { color: 'red', fontSize: '20px', fontWeight: 'bold' }
},
description: {
text: 'This is a warning message',
style: { color: 'orange', fontSize: '14px' }
}
})Usage with Express
// src/index.ts
import express from 'express'
import { logsGuard } from '@tent-official/logs-guard'
// Call logsGuard before any other code
logsGuard()
const app = express()
app.get('/', (req, res) => {
console.log('Request received') // This will be hidden in production
res.send('Hello World')
})
app.listen(3000, () => {
console.log('Server is running') // This will be hidden in production
})API
logsGuard(options?)
Guards (suppresses) specified console logs in production environment.
Parameters:
options(optional): Configuration objectsuppress: Array of console methods to suppress. Default:['log', 'info', 'debug']
Available log methods: 'log' | 'warn' | 'error' | 'info' | 'debug'
logsRelease(lines?)
Allows a limited number of log lines to be displayed before suppressing again.
Parameters:
lines(optional): Number of log lines to display. Default:1, Max:5, Min:0
How it works:
- After calling
logsGuard(), all logs are suppressed by default - After calling
logsRelease(n), the nextnlog statements will be displayed - After reaching the limit, logs will be suppressed again
- This is useful for debugging in production without showing all logs
Examples:
import { logsGuard, logsRelease } from '@tent-official/logs-guard'
// 1. Initially suppress all logs in production
logsGuard()
console.log('Log 1 - hidden') // Not displayed
console.log('Log 2 - hidden') // Not displayed
// 2. Allow 1 line to be displayed
logsRelease(1)
console.log('Log 3 - displayed') // Displayed (1st line)
console.log('Log 4 - hidden') // Not displayed (limit reached)
// 3. Allow up to 3 lines (max 5)
logsRelease(3)
console.log('Log 5 - displayed') // Displayed (1st line)
console.log('Log 6 - displayed') // Displayed (2nd line)
console.log('Log 7 - displayed') // Displayed (3rd line)
console.log('Log 8 - hidden') // Not displayed (limit reached)
// 4. Suppress all logs again
logsRelease(0)
console.log('Log 9 - hidden') // Not displayedlogsWarning(options)
Displays a styled warning message using console.log with CSS styling. Only works on Windows.
Parameters:
options: Configuration objecttitle(required): Text and style for the titletext: The text to displaystyle: CSS style object (e.g.,{ color: 'red', fontSize: '20px' })
description(optional): Text and style for the descriptiontext: The text to displaystyle: CSS style object
Supported CSS properties:
color, fontSize, fontWeight, background, padding, margin, etc.
Note: This function only works on Windows (process.platform === 'win32'). On other platforms, it does nothing.
Examples:
import { logsWarning } from '@tent-official/logs-guard'
// With title only
logsWarning({
title: {
text: 'Warning!',
style: { color: 'red', fontSize: '20px', fontWeight: 'bold' }
}
})
// With title and description
logsWarning({
title: {
text: 'Warning!',
style: { color: 'red', fontSize: '20px', fontWeight: 'bold' }
},
description: {
text: 'This is a warning message',
style: { color: 'orange', fontSize: '14px' }
}
})Behavior
| Environment | Behavior |
|-------------|----------|
| Production (NODE_ENV='production') | Suppresses specified logs |
| Development (other) | No effect, logs display normally |
Example
import { logsGuard } from '@tent-official/logs-guard'
// Suppress only 'log' and 'info'
logsGuard({ suppress: ['log', 'info'] })
console.log('This will be hidden in production') // Suppressed
console.info('This will also be hidden') // Suppressed
console.warn('Warning will still show') // Shows
console.error('Error will still show') // ShowsLicense
MIT
