Skip to content

Commit 58c16a3

Browse files
committed
chore: minor changes
1 parent 2a78eef commit 58c16a3

8 files changed

+119
-5433
lines changed

docs/sponsors.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ ___
7777
- Private Discord channel invite
7878
- Free Personal Account for Stacks Dashboard
7979
- Social media follow
80-
- Name listed in the `BACKERS.md` file of >70x `stacksjs/core` repos, `stacksjs/stacks`, `audiox`, `buddy.sh`, `bunfig`, `bun-plugin-auto-imports`, `bun-plugin-dtsx`, `bun-plugin-unocss`, `clarity`, `dnsx`, `docs.stacksjs.org`, `dtsx`, `httx`, `imgx`, `localtunnel.sh`, `qrx`, `reverse-proxy.sh`, `tlsx.sh`, `ts-collect`, `ts-spreadsheets`, `vite-plugin-local`, `vidx` and few others, above other individual backers.
80+
- Name listed in the `BACKERS.md` file of >70x `stacksjs/core` repos, `stacksjs/stacks`, `aax`, `audiox`, `buddy.sh`, `bun-git-hooks`, `bun-plugin-auto-imports`, `bun-plugin-dotenvx`, `bun-plugin-dtsx`, `bun-plugin-unocss`, `bunfig`, `clarity`, `dnsx`, `docs.stacksjs.org`, `dtsx`, `httx`, `imgx`, `jpgx`, `localtunnels.sh`, `pngx`, `post`, `qrx`, `reverse-proxy.sh`, `tlsx.sh`, `ts-avif`, `ts-cache`, `ts-clone`, `ts-collect`, `ts-countries`, `ts-gif`, `ts-maps`, `ts-ndarray`, `ts-punycode`, `ts-rate-limiter`, `ts-security`, `ts-spreadsheets`, `ts-svg`, `ts-vat`, `ts-webp`, `vidx`, `vite-plugin-dotenvx`, `vite-plugin-layouts`, `vite-plugin-local`, `vite-plugin-tauri`
8181

8282
___
8383

src/logger.ts

+87-7
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ interface ExtendedLoggerOptions extends LoggerOptions {
7878
showTags?: boolean // Enable/disable tags in console output
7979
tagFormat?: TagFormat // Custom format for tags
8080
timestampPosition?: 'left' | 'right' // Control timestamp position in console output
81+
environment?: string // Environment prefix for log entries
8182
}
8283

8384
export class Logger {
@@ -104,6 +105,7 @@ export class Logger {
104105
private fancy: boolean // Whether to use fancy terminal output
105106
private tagFormat: TagFormat
106107
private timestampPosition: 'left' | 'right'
108+
private environment: string // Environment prefix for log entries
107109
// eslint-disable-next-line no-control-regex
108110
private readonly ANSI_PATTERN = /\u001B\[.*?m/g // Use Unicode escape for ANSI sequence
109111
private activeProgressBar: { // State for the active progress bar
@@ -123,6 +125,7 @@ export class Logger {
123125
this.fancy = options.fancy ?? true // Enable fancy output by default
124126
this.tagFormat = options.tagFormat ?? { prefix: '[', suffix: ']' } // Default square bracket format
125127
this.timestampPosition = options.timestampPosition ?? 'right' // Default to left position
128+
this.environment = options.environment ?? process.env.APP_ENV ?? 'local' // Use APP_ENV or default to 'local'
126129

127130
// Initialize fingers-crossed config based on flag
128131
this.fingersCrossedConfig = this.initializeFingersCrossedConfig(options)
@@ -999,14 +1002,14 @@ export class Logger {
9991002
}
10001003
else if (!isBrowserProcess()) {
10011004
// Simple console output without styling
1002-
console.error(`${fileTime} ${level.toUpperCase()}: ${formattedMessage}`)
1005+
console.error(`${fileTime} ${this.environment}.${level.toUpperCase()}: ${formattedMessage}`)
10031006
if (errorStack) {
10041007
console.error(errorStack)
10051008
}
10061009
}
10071010

10081011
// Create the log entry for file logging
1009-
let logEntry = `${fileTime} local.${level.toUpperCase()}: ${formattedMessage}\n`
1012+
let logEntry = `${fileTime} ${this.environment}.${level.toUpperCase()}: ${formattedMessage}\n`
10101013
if (errorStack) {
10111014
logEntry += `${errorStack}\n`
10121015
}
@@ -1050,7 +1053,7 @@ export class Logger {
10501053
const consoleTime = this.formatConsoleTimestamp(timestamp)
10511054
const fileTime = this.formatFileTimestamp(timestamp)
10521055

1053-
let logEntry = `${fileTime} local.INFO: ${completionMessage}`
1056+
let logEntry = `${fileTime} ${this.environment}.INFO: ${completionMessage}`
10541057
if (metadata) {
10551058
logEntry += ` ${JSON.stringify(metadata)}`
10561059
}
@@ -1337,11 +1340,11 @@ export class Logger {
13371340
}
13381341
else if (!isBrowserProcess()) {
13391342
// Simple console output without styling
1340-
console.error(`${fileTime} BOX: ${message}`)
1343+
console.error(`${fileTime} ${this.environment}.INFO: [BOX] ${message}`)
13411344
}
13421345

13431346
// Write directly to file instead of using this.info()
1344-
const logEntry = `${fileTime} local.INFO: [BOX] ${message}\n`
1347+
const logEntry = `${fileTime} ${this.environment}.INFO: [BOX] ${message}\n`
13451348
await this.writeToFile(logEntry)
13461349
}
13471350

@@ -1477,7 +1480,7 @@ export class Logger {
14771480
// Log to file directly instead of using this.info()
14781481
const timestamp = new Date()
14791482
const formattedDate = timestamp.toISOString()
1480-
const logEntry = `[${formattedDate}] local.INFO: [START] ${formattedMessage}\n`
1483+
const logEntry = `[${formattedDate}] ${this.environment}.INFO: [START] ${formattedMessage}\n`
14811484

14821485
await this.writeToFile(logEntry)
14831486
}
@@ -1591,7 +1594,10 @@ export class Logger {
15911594
// Use a simpler icon for progress
15921595
const icon = isFinished || percent === 100 ? terminalStyles.green('✓') : terminalStyles.blue('▶')
15931596

1594-
const line = `\r${icon} ${bar} ${percentageText}${messageText}`
1597+
// Add tag if enabled
1598+
const tag = this.options.showTags !== false && this.name ? ` ${terminalStyles.gray(this.formatTag(this.name))}` : ''
1599+
1600+
const line = `\r${icon}${tag} ${bar} ${percentageText}${messageText}`
15951601

15961602
// Clear the rest of the line and write the new progress
15971603
const terminalWidth = process.stdout.columns || 80
@@ -1626,6 +1632,80 @@ export class Logger {
16261632

16271633
this.activeProgressBar = null // Clear the active state
16281634
}
1635+
1636+
/**
1637+
* Clears log files based on specified filters.
1638+
* @param filters Optional filters for clearing logs.
1639+
* @param filters.name A pattern to match logger names (e.g., 'api-*'). Only files matching this pattern will be considered.
1640+
* @param filters.before A Date object. Only log files with a last modified timestamp before this date will be deleted.
1641+
*/
1642+
async clear(filters: { name?: string, before?: Date } = {}): Promise<void> {
1643+
if (isBrowserProcess()) {
1644+
console.warn('Log clearing is not supported in browser environments.')
1645+
return
1646+
}
1647+
1648+
try {
1649+
console.warn('Clearing logs...', this.config.logDirectory)
1650+
const files = await readdir(this.config.logDirectory)
1651+
const logFilesToDelete: string[] = []
1652+
1653+
for (const file of files) {
1654+
// Basic check: Must be a log file associated with this logger instance's base name
1655+
// or match the provided name filter if any.
1656+
const nameMatches = filters.name
1657+
? new RegExp(filters.name.replace('*', '.*')).test(file)
1658+
: file.startsWith(this.name)
1659+
1660+
if (!nameMatches || !file.endsWith('.log')) {
1661+
continue // Skip files not matching the name or not ending in .log
1662+
}
1663+
1664+
const filePath = join(this.config.logDirectory, file)
1665+
1666+
// Date check: If 'before' filter is provided, check the file's last modified time.
1667+
if (filters.before) {
1668+
try {
1669+
const fileStats = await stat(filePath)
1670+
if (fileStats.mtime >= filters.before) {
1671+
continue // Skip files modified on or after the 'before' date
1672+
}
1673+
}
1674+
catch (statErr) {
1675+
console.error(`Failed to get stats for file ${filePath}:`, statErr)
1676+
continue // Skip if we cannot get stats
1677+
}
1678+
}
1679+
1680+
// If all checks pass, add the file to the deletion list.
1681+
logFilesToDelete.push(filePath)
1682+
}
1683+
1684+
if (logFilesToDelete.length === 0) {
1685+
console.warn('No log files matched the criteria for clearing.')
1686+
return
1687+
}
1688+
1689+
console.warn(`Preparing to delete ${logFilesToDelete.length} log file(s)...`)
1690+
1691+
// Delete the files
1692+
for (const filePath of logFilesToDelete) {
1693+
try {
1694+
await unlink(filePath)
1695+
console.warn(`Deleted log file: ${filePath}`)
1696+
}
1697+
catch (unlinkErr) {
1698+
console.error(`Failed to delete log file ${filePath}:`, unlinkErr)
1699+
}
1700+
}
1701+
1702+
console.warn('Log clearing process finished.')
1703+
}
1704+
catch (err) {
1705+
console.error('Error during log clearing process:', err)
1706+
// Optionally re-throw or handle the error more specifically
1707+
}
1708+
}
16291709
}
16301710

16311711
export const logger: Logger = new Logger('stacks')

0 commit comments

Comments
 (0)