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

console-writer

v1.0.4

Published

console auto write log file

Downloads

12

Readme

console-writer :zap:

license:MIT

This is a plugin that can write files simply through console.log

Install

# install package
npm install console-writer

Auto write file throw console

this is the entry file.

  1. usage1 index.js
import {auto} from 'console-writer';
auto();
console.log('first write!');
  1. usage2 index.js
import 'console-writer/auto';
console.log('first write!');

The logs/console.log file will be generated under the project

Auto write file and config it

You need to prepare a cw.config.js file in the project root directory

cw.config.js

module.exports = {
    option: {
        logDir: 'console/logs', // Based on the log file directory in the project directory
        fileName: 'run.log', // The log file name
        disabledConsole: false, // Disable console output
        level: 'all', // log level : all > debug > info > warn > error > off
        backup: true, // Backup the log
        backupSize: 100 * 1024 * 1024, // Maximum size of the backup file, if backup = true
        backupZip: false, // Packing backup files, if backup = true
        backupCount: 20, // Maximum number of backup files
        autoRewrite: true, // if false : You can actively call rewriteConsole to intercept the console
        autoInitOption: true
    }
}

Instantiate the console-writer

You can configure console-writer by instantiating it

  1. Usage 1

this is the entry file.

index.js

import {ConsoleBuilder,Level} from 'console-writer';
const instance = new ConsoleBuilder({
    logDir: 'console/logs',
    fileName: 'run.log',
    disabledConsole: false,
    level: Level.all,
    backup: true,
    backupSize: 100 * 1024 * 1024,
    backupZip: false,
    backupCount: 20,
    autoRewrite: false, // if false : You can actively call rewriteConsole to intercept the console
    autoInitOption: true
});
console.log('before write!');
instance.rewriteConsole(); // Actively call rewriteConsole
console.log('after write!');

You can build multiple instances of ConsoleBuilder with different config throw "autoRewrite = false"

  1. Usage 2

this is the entry file.

index.js

import {ConsoleBuilder,Level} from 'console-writer';
const instance = new ConsoleBuilder({
    autoRewrite: false,
    autoInitOption: false // // if false : You can reset option
});
instance.initOption({
    logDir: 'console/logs',
    fileName: 'run1.log',
    disabledConsole: false,
    level: Level.all,
    backup: true,
    backupSize: 100 * 1024 * 1024,
    backupZip: false,
    backupCount: 20,
})
instance.rewriteConsole();
console.log('first write!'); // This record is written to the run1.log file
// reset option
console.initOption({
    logDir: 'console/logs',
    fileName: 'run2.log',
    disabledConsole: false,
    level: Level.all,
    backup: true,
    backupSize: 100 * 1024 * 1024,
    backupZip: false,
    backupCount: 20,
})
instance.rewriteConsole();
console.log('second write!'); // This record is written to the run2.log file
  1. Usage 3

throw config file this is the entry file.

index.js

import {ConsoleBuilder} from 'console-writer';
new ConsoleBuilder({
  useConfig: true,
  configFileName: "cw_conf.js" // The default file name is cw.config.js
});
console.log('log write!');

Hooks

If you got a instance of ConsoleBuilder, You can trigger the callback method when the log is output

import {ConsoleBuilder} from 'console-writer';
const {onLog,onInfo,onWarn,onError,onDebug} = new ConsoleBuilder({useConfig: true});
// when console.log()
onLog((console)=>{
    console.log('before log write'); // This sentence will don't write in the log file
    return (console) => {
        console.log('after log console print'); // This sentence will don't write in the log file
    }
})
// when console.info()
onInfo(console=>{
    // ...
    return (console) => {
        // ...
    }
})
// when console.warn()
onWarn(console=>{
    // ...
    return (console) => {
        // ...
    }
})
// when console.error()
onError(console=>{
    // ...
    return (console) => {
        // ...
    }
})
// when console.debug()
onDebug(console=>{
    // ...
    return (console) => {
        // ...
    }
})