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

logcalltracer

v1.1.5

Published

A minimalistic library that shows the caller's location in log messages, with an optional timestamp, log level.

Downloads

6

Readme

logcalltracer

NPM Version

A minimalistic library that shows the caller's location in log messages, with an optional timestamp, log level.

Usage

You can use the following methods, the same way you would use console.log, .warn, and .error methods:

  • logInfo
  • logWarn
  • logError

The default output is the caller's location, then the values of the parameters. Example:

// Import module
const logger = require("logcalltracer");

logger.logInfo("Hello world!")

shows the following output:

/myApp/start.js:4: Hello world!

Supported environments

  • Node.js >= 12.22.1
  • Deno >= 1.12.0

This package is intended to be used in server applications based on Node.js and Deno. Browsers are not supported, but logcalltracer seems to work with:

  • Brave 1.27.108 Chromium: 92.0.4515.107 (Official Build) (64-bit)
  • Chrome 91.0.4472.164 (Official Build) (64-bit)
  • Edge 92.0.902.55 (Official build) (64-bit)
  • FireFox 90.0.2 (64-bit)

Customizing output

The library can be configured to provide additional information, such as timestamp, log level, coloring, and so on. Use the following properties on the options object:

timeStampFormatter : Function

A method to get a formatted timestamp. The default value is undefined, so log output has no timestamp. You can use the built-in Date object or any 3rd party library to provide a method that returns your preferred timestamp.

Example:

// Import module
const logger = require("logcalltracer");

logger.options.timeStampFormatter = () => new Date().toISOString();

logger.logInfo("Hello world!")

shows the following output:

2021-06-04T11:57:43.175Z  /myApp/start.js:4: Hello world!

showLogLevel: boolean

When set to true, the output displays the log level.

inspectOptions: object

This library uses util.inspect to format arguments. You can change the default formatting behavior using the inspectOptions: https://nodejs.org/dist/latest-v16.x/docs/api/util.html#util_util_inspect_object_options

For a development environment, I suggest the following to start with:

const logger = require("logcalltracer");
logger.options.inspectOptions = {compact: false, depth: Infinity};

logInfoFn: Function, and logWarnFn, logErrorFn

By default, the methods are mapped to the console's logging methods: | Method | Calls | |--- |--- | |logInfo | console.log(timestamp?, info?, callerLocation, ...arguments) | |logWarn | console.warn(timestamp?, warn?, callerLocation, ...arguments) | |logError| console.error(timestamp?, err?, callerLocation, ...arguments) |

You can plug in your own, or an external logging library's logger methods. Example:

const logger = require("logcalltracer")
const util = require("util")
const fs = require("fs");

logger.options.logInfoFn = function(timestamp, logLevel, callerLocation, ...rest) {
    console.log("My custom logging method");
    fs.appendFile(
        "test.log",
        `${timestamp} ${logLevel} ${callerLocation} ${util.formatWithOptions.apply(null, [logger.options.inspectOptions, ...rest])}\n`,
        "utf8",
        err => {});
}

logger.options.timeStampFormatter = () => new Date().toISOString();
// logger.options.showLogLevel is not set, so it won't be passed to the logInfoFn method.
logger.logInfo("Application starting..", { a: 42, b: { c: 'Hi!'}});

will create a file with the following content:

2021-06-04T12:26:09.455Z  /myApp/start.js:16: Application starting.. { a: 42, b: { c: 'Hi!' } }

logLevelColored: get/set(boolean)

By default, no coloring is performed on the output. When both showLogLevel and logLevelColored are set to true, the log level is colored as follows: | Method | Color | |--- |--- | |logInfo | None | |logWarn | Yellow| |logError| Red |

An example with options

// Import module
const logger = require("logcalltracer");

// Customize the behavior if you want to.
if (process.env.NODE_ENV === "production") {
  logger.options.timeStampFormatter = () => new Date().toISOString();
  logger.options.showLogLevel = true;
  logger.options.inspectOptions = {compact: false};
} else {
  logger.options.timeStampFormatter = () => new Date().toLocaleTimeString();
  logger.options.inspectOptions = {compact: false, depth: Infinity};
  logger.options.logLevelColored = true;
  logger.options.showLogLevel = true;
}

logger.logInfo("Application starting..", { a: 42, b: { c: 'Hi!'}});

shows the following output:

1:42:13 PM INFO  c:\myApp\start.js:16: Application starting.. {
  a: 42,
  b: {
    c: 'Hi!'
  }
}

License

MIT