diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..40b878d --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +node_modules/ \ No newline at end of file diff --git a/README.md b/README.md index 468464f..9afc130 100644 --- a/README.md +++ b/README.md @@ -6,4 +6,32 @@ Just Node.js saying hello to the world and such. +

Maddie Rajavasireddy

+

Description:

+ +

Building the Logger

+ +Create a logger module. Logger will need the following features: + +A log method that takes 2 parameters +The first parameter should be the message to log +The second parameter should be a string specifying the logging level (INFO, WARNING, ERROR) +An info method that only takes a single parameter for the message and outputs it at the info level +A warning method that only takes a single parameter for the message and outputs it at the warning level +An error method that only takes a single parameter for the message and outputs it at the error level + +Based on the logging level, colorize your output using chalk according to the following chart +INFO: blue +WARNING: yellow +ERROR: red + +

Logging from a JSON File

+ +Now that the logger is built and ready to use, require the JSON file in the data/ folder so that one can output the messages. Each message has a "level" key so be sure to output the message using the correct level in the logger! + +Require the JSON file +Require lodash to make iterating over the JSON object easier +Use the lodash _.each function to iterate over the JSON +Output the message for each entry at the given logging level +Verify that the JSON is output correctly and each message is colorized according to its logging level diff --git a/index.js b/index.js new file mode 100644 index 0000000..600bb17 --- /dev/null +++ b/index.js @@ -0,0 +1,8 @@ +var _ = require("lodash"); //get lodash +var logs = require("./data/logs"); +var logger = require("./lib/logger"); + +//loop through the logs and print to console +_.each(logs, function(log){ + logger.log(log.message, log.level); +}); \ No newline at end of file diff --git a/lib/logger.js b/lib/logger.js new file mode 100644 index 0000000..693e5ee --- /dev/null +++ b/lib/logger.js @@ -0,0 +1,30 @@ +const chalk = require("chalk"); //get chalk + +var logger = { + log: function(message, level){ + //call different method based on log level + switch(level) { + case "info": + this.info(message); + break; + case "warning": + this.warning(message); + break; + case "error": + this.error(message); + } + + }, + info: function(message){ + console.log(chalk.blue(message)); //info -> message in blue + }, + warning: function(message){ + console.log(chalk.yellow(message)); //info -> message in yellow + }, + error: function(message){ + console.log(chalk.red(message)); //info -> message in red + } +}; + +//export logger +module.exports = logger; \ No newline at end of file diff --git a/package.json b/package.json new file mode 100644 index 0000000..d2e8323 --- /dev/null +++ b/package.json @@ -0,0 +1,23 @@ +{ + "name": "assignment_node_hello_world", + "version": "1.0.0", + "description": "assignment_node_hello_world ===========================", + "main": "index.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/maddiereddy/assignment_node_hello_world.git" + }, + "author": "Maddie Rajavasireddy", + "license": "ISC", + "bugs": { + "url": "https://github.com/maddiereddy/assignment_node_hello_world/issues" + }, + "homepage": "https://github.com/maddiereddy/assignment_node_hello_world#readme", + "dependencies": { + "chalk": "^2.1.0", + "lodash": "^4.17.4" + } +}