npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@siteed/react-native-logger

v1.0.1

Published

A simple yet powerful logging library for cross platform applications, supporting persistent log history.

Downloads

5,181

Readme

@siteed/react-native-logger

kandi X-Ray Version Codecov Dependency Status License

@siteed/react-native-logger is a simple, yet powerful logging library designed for React Native applications. It extends the basic console logging functions by maintaining a log history that can be displayed within your app or exported for troubleshooting.

Installation

npm install @siteed/react-native-logger
yarn add @siteed/react-native-logger

Key Features

  • Persistent Log History: Keeps a history of log messages that can be displayed in-app for easier debugging and diagnostics.
  • Production Debugging: Facilitates debugging in production by allowing logs to be reviewed directly from a device.
  • Configurable Maximum Logs: Set the maximum number of logs kept in memory to prevent overflow.
  • Namespace-Based Logging: Enable or disable logging for specific namespaces based on environment variables or local storage settings.

Usage

To get started with @siteed/react-native-logger, configure the logger settings and use the logging functions within your React components or outside of them.

Recommended Setup

It is recommended to create a base logger for your project and extend it for any specific features or screens. This approach allows you to isolate logging per application or feature while remaining compatible if an external library also uses a different namespace.

Basic Setup

import { getLogger, setLoggerConfig } from '@siteed/react-native-logger';

// Set logger configuration
setLoggerConfig({ maxLogs: 500, namespaces: 'App:*' }); // Set the maximum number of logs to 500 and enable logging for App namespace

// To use outside react component, you can call getLogger directly
const logger = getLogger('App');
logger.debug('This is a debug message');
logger.info('This is an info message');
logger.warn('This is a warning message');
logger.error('This is an error message');

// Creating a sub-logger
const subLogger = logger.extend('Sub');
subLogger.info('This is a message from the sub-logger');

const App = () => {

  useEffect(() => {
    logger.log('App mounted');
    subLogger.debug('App component mounted');
  }, []);

  return (
    <View>
      <Text>App</Text>
    </View>
  );
};
export default App;

Activating logs with debug compatibility

react-native-logger is compatible with the debug package, allowing you to enable or disable logging for specific namespaces based on environment variables or local storage settings. This is particularly useful for controlling log output in different environments (development, staging, production).

Environment Variables (nodejs)

To activate logs for specific namespaces using environment variables:

# Enable all logs
export DEBUG=*

# Enable logs for specific namespaces
export DEBUG=namespace1,namespace2

Local Storage (web)

To activate logs for specific namespaces using local storage (e.g., in a browser environment):

// Enable all logs
localStorage.setItem('DEBUG', '*');

// Enable logs for specific namespaces
localStorage.setItem('DEBUG', 'namespace1,namespace2');

Accessing logs in Production

@siteed/react-native-logger is particularly useful in production, where traditional debugging tools are not accessible. For instance, you can create a dedicated screen within your app that displays log history, allowing users to copy and send these logs for support purposes, or even set up automatic log forwarding via email or a web service.

import { getLogger, getLogs, clearLogs } from '@siteed/react-native-logger';
import React, { useEffect, useState } from 'react';
import { View, Text, Button, FlatList } from 'react-native';

const LogScreen = () => {
  const [logs, setLogs] = useState(getLogs());

  const handleSendLogs = async () => {
    const logData = logs.map(log => `${new Date(log.timestamp).toLocaleString()}: ${log.message}`).join('\n');
    await sendLogsToSupport(logData); // Define this function to match your backend support system
  };

  useEffect(() => {
    setLogs(getLogs()); // Refresh logs when component mounts
  }, []);

  const renderItem = ({ item }: ListRenderItemInfo<typeof logs[0]>) => (
    <View style={styles.logEntry}>
      <View>
        <Text style={styles.timestamp}>
          {new Date(item.timestamp).toLocaleTimeString()}
        </Text>
        <Text style={styles.context}>{item.namespace}</Text>
      </View>
      <Text style={styles.message}>{item.message}</Text>
    </View>
  );

  return (
    <View style={styles.container}>
       <FlatList
        data={filteredLogs}
        renderItem={renderItem}
        keyExtractor={(item, index) => `${index}-${item.timestamp}`}
        style={styles.viewer}
        initialNumToRender={20} // Adjust based on performance requirements
      />
      <Button title="Send Logs to Support" onPress={handleSendLogs} />
    </View>
  );
};

export default LogScreen;

License

MIT