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

google-cloud-logger

v1.0.4

Published

Comfortable logging in google cloud

Downloads

19

Readme

WORK IN PROGRESS ...

How it works

The package overrides default handling of console log/debug/warn and error. If object is provided as argument, it will put the object in jsonPayload of log entry instead of dumping the object on multiple lines.

It is also possible to provide additional labels (e.g. sessionId, executionId, endPoint,git info, etc). Just add following somewhere into your main express/axios script

import { AsyncLocalStorageContext as appNamespace } from '@northscaler/continuation-local-storage'; // prerequisite:  Node.js >= 12.17.0

appNamespace().run(() => {
    appNamespace().set("logEntryMetadata", {
        executionId: uuidv4(),
        endPoint: endpointName,
        "git.branch": GIT_BRANCH,
        "git.commit": GIT_COMMIT
     });
    endpointFunction(req, res)
})

you can update labels for any subsequent log entries by using

cloudLogger.setLogEntryMetadata({consumerId:12345}, '');

How to install

npm i google-cloud-logger

Important Note

If the AppEngine project is different to project where you want to log, make sure the AppEngine service account has IAM logs write access to target project. Also, Cloud Functions are using by default the same service account as AppEngine.

Example of How to use

Example below is using google AppEngine environment variable GAE_INSTANCE. If the variable is set, it means the solution runs on google cloud and every logging into console will use the google-cloud-logger. If not set, then standard logging is used (e.g. when running locally)

Every log entry will also include AppEngine module, version and instance id.

import cloudLogger from "google-cloud-logger";

const {
    GAE_SERVICE,
    GAE_VERSION,
    GAE_INSTANCE
} = process.env

const CLOUD_LOGGER_PROJECT_ID = 'solaris-qa-consumer';
const CLOUD_LOGGER_DEFAULT_LOG_NAME = 'google-cloud-test';
const CLOUD_LOGGER_RESOURCE_TYPE = 'gae_app';
const IS_RUNNING_ON_CLOUD = GAE_INSTANCE ? true : false;


const options = {
    isRunningOnCloud: IS_RUNNING_ON_CLOUD,
    projectId: CLOUD_LOGGER_PROJECT_ID,
    logName: CLOUD_LOGGER_DEFAULT_LOG_NAME,
    defaultLoggerMetadata: {
        resource: {
            type: CLOUD_LOGGER_RESOURCE_TYPE,
            labels: {
                module_id: GAE_SERVICE,
                version_id: GAE_VERSION,
                instance_id: GAE_INSTANCE
            }
        },
        labels: {}
    }
}

cloudLogger.init(options);

console.log("This is logging of simple text");
console.log(options); //This is logging of object
console.debug("This is logging of text and object:", options);
console.warn("This is a warning");
console.error("This is an error:", new Error());

How it looks in google StackDriver log

StackDriver log StackDriver log