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

@envoy/loglevel-file-logger

v1.0.8

Published

File logger for loglevel

Downloads

10

Readme

loglevel-file-logger

File logger for loglevel. This package is currently for React Native, it relies on react-native-fetch-blob. We didn't add react-native-fetch-blob in dependencies, as we are thinking maybe it's possible to open up this library to use different file library, so you need to install react-native-fetch-blob by yourself.

Usage

Install react-native-fetch-blob if it's not a dependency in your project yet.

yarn add react-native-fetch-blob

Then you can install loglevel-file-logger like this

yarn add loglevel-file-logger

Next, import it in the entry point file of your app.

import setupLogger, { FetchBlobWriter } from 'loglevel-file-logger'

// Notice: this is a hack, somehow
//
//     import * as Log from 'loglevel'
//
// Will clone the module.exports object, which is the default logger.
// As we want to change `methodFactory` of the original object instead of
// cloned one, we import the logger like this instead.
const defaultLogger: Log = require('loglevel')

export default class App extends Component {
  private readonly fileWriter: FetchBlobWriter

  constructor (props: any, context?: any) {
    super(props, context)
    this.fileWriter = new FetchBlobWriter()
    this.fileWriter.ensureFile().then(() => {
      // make linter not to complain about promise not subscribed
    })
    // setup the file logger
    setupLogger({
      log: defaultLogger,
      write: this.fileWriter.write.bind(this.fileWriter)
    })
    // enable all logger levels
    defaultLogger.enableAll()
}

Please notice that we use require('loglevel') to import the global root logger from loglevel, as if you use import * as Log from 'loglevel', somehow it will copy the export object and we won't be able to replace the original methodFactory of it. Also, you need to call setupLogger from loglevel-file-logger before calling enableAll on the root logger, as enableAll replaces logging methods for all logger, you need to setup file logger first otherwise it won't replace method with the one which writes to file.

After the logger setup, you can the logger from loglevel and write as much as log you want

const logger = Log.getLogger('my-logger')
logger.info('Hello world')

FetchBlobWriter

FetchBlobWriter is the only log writer we support now, it relies on react-native-fetch-blob. By default the log file path is

RNFetchBlob.fs.dirs.DocumentDir + '/log.txt'

To get the path in Objective C, you can write

NSArray<NSString *> *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentDir = paths[0];
NSString *logFile = [documentDir stringByAppendingPathComponent:@"log.txt"];

You can pass an alternative path into constructor. Like this

this.fileWriter = new FetchBlobWriter('/path/to/my/log')

The default log formatter we use is defaultFormatter from

import { defaultFormatter } from 'loglevel-file-logger'

You can define your own by implementing Formatter interface and pass it to constructor as well.

this.fileWriter = new FetchBlobWriter('/path/to/my/log', myFormatter)

TODO

  • [ ] FetchBlobWriter with file rotation, we can keep say like 7 days of logs