5.0.2 • Published 3 months ago
react-script-log v5.0.2
# react-script-log
`react-script-log` is a simple and flexible logging solution for React applications. This package provides an easy way to log messages to the console with different levels (info, warning, error) and allows for customizable logging formats.
## Installation
You can install `react-script-log` using npm or yarn:
```bash
npm install react-script-log
or
yarn add react-script-log
Getting Started
To start using react-script-log
, import it into your React component or utility file where you want to log messages.
import { logger } from 'react-script-log';
Usage
Basic Logging
You can use the logger to log messages of different severity levels:
logger.info('This is an info message.');
logger.warn('This is a warning message.');
logger.error('This is an error message.');
Custom Logging Function
You can also supply a custom logging function for more control over how logs are handled:
const customLogger = (message) => {
// Customize your logging here (e.g., send to an API)
console.log(`Custom Log: ${message}`);
};
logger.setLogger(customLogger);
logger.info('This message will go through the custom logger.');
Log Formatting
Customize the log format by providing options during initialization:
logger.configure({
format: (level, message) => `[${level.toUpperCase()}]: ${message}`
});
logger.info('Formatted message with custom logger.');
API
logger.info(message: string): void
Logs an informational message.
logger.warn(message: string): void
Logs a warning message.
logger.error(message: string): void
Logs an error message.
logger.setLogger(loggerFunc: Function): void
Sets a custom logging function.
logger.configure(options: Object): void
Configures logger options, including log format.
Example
Here is a simple example of using react-script-log
in a React component:
import React from 'react';
import { logger } from 'react-script-log';
const MyComponent = () => {
const handleClick = () => {
logger.info('Button clicked!');
};
return (
<button onClick={handleClick}>
Click Me
</button>
);
};
export default MyComponent;