mowispace / react-native-logs

Performance-aware simple logger for React-Native and Expo with namespaces, custom levels and custom transports (colored console, file writing, etc.)
MIT License
467 stars 33 forks source link

Improve type definitions #109

Closed DanielSRS closed 1 month ago

DanielSRS commented 1 month ago

This PR aims to improve type checking, mostly when setting a transport function.


Screenshot 2024-10-22 at 18 28 54

Screenshot 2024-10-22 at 18 26 36

The most notable change in this PR is that transportFunctionType is now a generic type, to when previously creating a custom transport could be done like this:

var customTransport: transportFunctionType = (props) => {
  // Do here whatever you want with the log message
  // Eg. a console log: console.log(props.level.text, props.msg)
};

will now be like this:

var customTransport: transportFunctionType<{ myCustomOption: string }> = (props) => {
  // Do here whatever you want with the log message
  // Eg. a console log: console.log(props.level.text, props.msg)
};

alessandro-bottamedi commented 1 month ago

@DanielSRS Wow, awesome job! I’m testing all the code, and there’s a modification to make in the fileAsyncTransport regarding the method checks, but really great work. To complete it, do you have any ideas on how to retrieve the levels types directly from the configuration instead of setting them manually?

DanielSRS commented 1 month ago

@alessandro-bottamedi Just added it

Now instead of doing:

const config = {
  levels: {
    trace: 0,
    info: 1,
    error: 2,
  },
};

var log = logger.createLogger<"trace" | "info" | "error">(config);

Providing the config should be enough. If not config is provided, the default ones are used instead

const config = {
  levels: {
    trace: 0,
    info: 1,
    error: 2,
  },
} as const;

var log = logger.createLogger(config);
log.trace // should be correctly typed

var defaultLog = logger.createLogger();
defaultLog.warn // also correctly typed