Skip to content

LogDNA logger for Node. Originally created for Cloudflare worker

Notifications You must be signed in to change notification settings

oligirling/logdna-logger

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 

Repository files navigation

Node Logger for LogDNA

What?

This is a logger class that allows you to log your request in LogDNA

LogDNA api docs can be found here

The following parameters are logged from the request:

  • user agent
  • referer
  • ip
  • countryCode
  • url
  • colo
  • request method
  • x_forwarded_for
  • asn
  • CF-Ray
  • tlsCipher
  • tlsVersion
  • clientTrustScore

Why?

This was originally created to use and log requests coming into a CloudFlare Worker. However it can be used for other requests.

Cloudflare logs are only available on enterprise accounts. So thanks to boynet's cf-logdna-worker concept, I decided to make something similar to fit my needs. Check theirs out as well as it maybe more suitable.

I needed a logger to log each request as they came in and to add meta data (eg users apiKeys, details from CloudFlares KV, responseTime etc) along the way, so I created this.

How?

Firstly you need to add your details to the logger. The following places need adjusting before using this class:

  • applicationName (top of the file)
  • token (inside postRequest())
  • hostname (inside postRequest())
  • tagName (inside postRequest())

To use this logger just bring the class in:

const logDnaLogger = require('./Logger')

And create the object - passing in a Request object

const logger = new logDnaLogger(request)

You can add custom meta params using the method:

Logger.setMeta('userApiKey', 'VALUE')

Adding meta like this will add meta value to any log that goes after this.

To start logging, use the log level methods:

Logger.info('Request Finished')

Available level methods:

  • info()
  • debug()
  • error()

More can be added if you require

Just before the response is returned, fire the logs up to LogDNA.

await Logger.postRequest()

This will post all the logs up in one batch with the meta you have added.

This has been tested using CloudFlare Workers and is working well.

Screenshots in LogDna

Below is a screenshot of how a log entry from this logger shows up in LogDNA, with a bunch of custom meta added using setMeta() (eg output, endpoint etc)

Screenshot 2020-12-08 at 09 31 36

Using the custom meta sent from the logger, you can create graphs and other visual tools. See below:

Screenshot 2020-12-08 at 09 31 03

Examples

Cloudflare worker example:

const logDnaLogger = require('./Logger')

addEventListener('fetch', event => {
    event.respondWith(handleRequest(event.request))
})

/**
 * Handle the request
 *
 * @param request
 * @returns {Promise<Response>}
 */
async function handleRequest(request) {    
    const logger = new logDnaLogger(request)
    try {
        logger.setMeta('day', 'Monday')
        logger.info('Request received')
        throw new Error('Something went wrong')
    } catch (e) {
        logger.error(e.message)
    } finally {
      await logger.postRequest()
    }
}

This example would send 2 logs in a single call to LogDNA. First would be of INFO level and the second would be an ERROR. Both the logs would have the meta added, the day as well as all the default meta values mentioned above.

About

LogDNA logger for Node. Originally created for Cloudflare worker

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published