@xcelsior/strapi-logger
v1.0.1
Published
Enhanced logging plugin for Strapi CMS in the Xcelsior ecosystem.
Readme
@xcelsior/strapi-logger
Enhanced logging plugin for Strapi CMS in the Xcelsior ecosystem.
Installation
pnpm add @xcelsior/strapi-loggerFeatures
Basic Setup
Add to your Strapi configuration:
// config/plugins.ts
export default {
'xcelsior-logger': {
enabled: true,
config: {
level: 'info',
format: 'json',
service: 'strapi-cms',
},
},
};Logging Usage
Use the enhanced logger in your Strapi code:
// In your controllers/services/middlewares
export default {
async find(ctx) {
strapi.log.info('Fetching entries', {
contentType: 'article',
query: ctx.query,
});
try {
const entries = await strapi.entityService.findMany('api::article.article');
return entries;
} catch (error) {
strapi.log.error('Failed to fetch entries', {
error: error.message,
stack: error.stack,
});
throw error;
}
},
};Request Logging
Automatic request logging middleware:
// config/middlewares.ts
export default [
// ... other middlewares
{
name: 'xcelsior-logger',
config: {
requestLogging: true,
excludePaths: ['/admin', '/uploads'],
},
},
];Configuration
Plugin Options
interface LoggerOptions {
level?: 'debug' | 'info' | 'warn' | 'error';
format?: 'json' | 'pretty';
service?: string;
requestLogging?: boolean;
excludePaths?: string[];
transports?: {
console?: boolean;
file?: {
enabled: boolean;
path: string;
maxSize: string;
maxFiles: number;
};
};
}Environment Variables
The plugin respects these environment variables:
STRAPI_LOGGER_LEVEL: Sets the logging levelSTRAPI_LOGGER_FORMAT: Sets the output formatSTRAPI_LOGGER_PATH: Sets the log file path
Advanced Usage
Custom Formatters
Add custom log formatters:
// config/plugins.ts
export default {
'xcelsior-logger': {
config: {
formatters: {
custom: (info) => {
return `[${info.timestamp}] ${info.level}: ${info.message}`;
},
},
},
},
};Context Tracking
Add context to your logs:
// In your code
strapi.log.withContext({
user: ctx.state.user?.id,
contentType: 'article',
}).info('Creating new entry');Performance Monitoring
Track operation performance:
const timer = strapi.log.startTimer();
try {
await someOperation();
} finally {
timer.done({
message: 'Operation completed',
operation: 'someOperation',
});
}Best Practices
Error Logging
try {
await riskyOperation();
} catch (error) {
strapi.log.error('Operation failed', {
error: {
message: error.message,
stack: error.stack,
code: error.code,
},
context: {
operation: 'riskyOperation',
input: input,
},
});
throw error;
}Sensitive Data
strapi.log.info('User action', {
user: {
id: user.id,
email: strapi.log.mask(user.email),
// Don't log passwords or sensitive data
},
});Request Context
module.exports = (config, { strapi }) => {
return async (ctx, next) => {
const requestLogger = strapi.log.child({
requestId: ctx.request.id,
method: ctx.request.method,
path: ctx.request.path,
});
try {
await next();
} finally {
requestLogger.info('Request completed', {
status: ctx.response.status,
duration: Date.now() - ctx.request.startTime,
});
}
};
};License
MIT
