1.0.1 • Published 6 years ago

ollyutils v1.0.1

Weekly downloads
2
License
ISC
Repository
github
Last release
6 years ago

OLLYUTILS v0.1

PLEASE NOTE THIS IS STILL IN DEVELOPMENT

A simple package for logging and managing environment variables.

The package consists of 2 utilities:

1. Environment config

Overview

Environment config allows you to maintain a set of environment config within your own project using a 'config.json' file at the root of your project directory.

Basic use

In order to use Environment config you will need to have a base level file in your project called 'config.json'. This file will need to be a basic json file with an element for each environment you intend to use. For example:

{
    "dev": {
        "name" : "development",
        "port" : 1234
    },

    "prod": {
        "name" : "production",
        "port" : 8080
    }
}

When you want to use an environment variable in your code you will need to require the utils package:

var utils = require('ollyutils')

and then create reference your config as follows:

var config = utils.config

The value of the environment you are using is set as an npm environment variable called 'environment'. You will need to set this before running your script as follows:

npm config set environment dev

This will set the environment as 'dev' and correspond to the 'dev' object in your config.json file.

If no environment variable is set the default value will be 'dev'. If no element in your config.json file corresponds to your environment variable you will receive an undefined object and an error will be logged.

2. Logging

Overview

Logging is a simple framework for logging some basic information to aid with support, debugging and monitoring. It logs in the following format:

[timestamp] | [calling file] | log text

For example:

07/03/2018 09:13:04:222 | index.js | "Service initiated"

Basic Use

First you will need to require ollyutils:

var utils = require('ollyutils')

Then you can define your log constant:

var log = utils.log;

The log function is then called using 2 parameters.

  1. The level of logging A number from 1-5 where 1 = Info and 5 = Silly on the logging scale. Your log level must be set as an environment variable called 'LOG_LEVEL'. If this parameter doesn't exist it will default to '1'

    You can set this value using the command:

    npm config set LOG_LEVEL 3

    Where '3' is the level of logging you want to set

    In reality this number can be greater than 5 or less than 1 if you wish to have more logging levels.

  2. Your log text This can be either plain text or JSON, which will be stringified.

    For example you would log at silly level by calling:

    log(5 , 'This is some arbitrary information')

    and provided the log level is greater than or equal to 5 the following would be outputted:

    07/03/2018 09:13:04:222 | index.js | "This is some arbitrary information"

An example

The following would use the environment variable passed as a parameter (this could also be set in a separate file) to start a basic express endpoint on a specified port:

index.js:

//Create the utils objects
var utils = require('ollyutils')
var config = utils.environment(process.argv[2]);
var env = config.ENVIRONMENT;
var log = utils.log

//Require express
var express = require('express');
var app = express();

app.get('/', function (req, res) {
	res.status(200).send('Look Mum! I dun it!')
})

var port = config.PORT
app.listen(port);
log(1,'Listening on port ' + port);

Command:

node index.js dev

Output:

07/03/2018 09:47:14:143 | index.js | "Listening on port 12345"
1.0.1

6 years ago

1.0.0

6 years ago

0.1.3

6 years ago

0.1.2

6 years ago

0.1.1

6 years ago

0.1.0

6 years ago

0.0.7

6 years ago

0.0.6

6 years ago

0.0.5

6 years ago

0.0.4

6 years ago

0.0.3

6 years ago

0.0.2

6 years ago

0.0.1

6 years ago