From 857ac5e95e43559ee8abfb143997b056d24dab23 Mon Sep 17 00:00:00 2001 From: "Zed A. Shaw" Date: Wed, 20 Jul 2022 13:58:38 -0400 Subject: [PATCH] First layout of Ex02 but no code quite yet. --- 02-filter-a-log-file/README.md | 11 +++++++++++ 02-filter-a-log-file/package.json | 15 +++++++++++++++ 02-filter-a-log-file/short.log.txt | 10 ++++++++++ 02-filter-a-log-file/step1.js | 7 +++++++ 4 files changed, 43 insertions(+) create mode 100644 02-filter-a-log-file/package.json create mode 100644 02-filter-a-log-file/short.log.txt create mode 100644 02-filter-a-log-file/step1.js diff --git a/02-filter-a-log-file/README.md b/02-filter-a-log-file/README.md index e69de29..7083303 100644 --- a/02-filter-a-log-file/README.md +++ b/02-filter-a-log-file/README.md @@ -0,0 +1,11 @@ +# 02: Filter a Log File + +In this exercise you'll take a webserver log I've created and parse the log file then filter the log for special events or information. You have a couple of approaches: + + +1. Brute force the filter by simply using a regex to scan the raw text. +2. Parse each line into meaningful data and filter on that. + +The two samples in this directory get you started with both approaches. The `step1.js` file starts off the brute force `RegEx` method while the `step2.js` starts off the parsing method. + +Keep in mind these are not solutions. They're starter file so you can get going without having to struggle with all of the setup. Once you're done you should attempt this again but do it entirely from nothing so you know how that's done. diff --git a/02-filter-a-log-file/package.json b/02-filter-a-log-file/package.json new file mode 100644 index 0000000..28d2f2b --- /dev/null +++ b/02-filter-a-log-file/package.json @@ -0,0 +1,15 @@ +{ + "name": "02-filter-a-log-file", + "version": "1.0.0", + "description": "", + "type": "module", + "main": "step1.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "author": "", + "license": "BSD", + "dependencies": { + "ava": "^4.3.1" + } +} diff --git a/02-filter-a-log-file/short.log.txt b/02-filter-a-log-file/short.log.txt new file mode 100644 index 0000000..41c0c06 --- /dev/null +++ b/02-filter-a-log-file/short.log.txt @@ -0,0 +1,10 @@ +1.1.1.1 - - [20/Jul/2022:16:15:55 +0200] "GET /6ce04bf8.js HTTP/2.0" 200 1420 "https://learnjsthehardway.com/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/1.1.1.1 Safari/537.36" "-" +1.1.1.1 - - [20/Jul/2022:16:15:55 +0200] "GET /e0335a10.js HTTP/2.0" 200 410 "https://learnjsthehardway.com/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/1.1.1.1 Safari/537.36" "-" +1.1.1.1 - - [20/Jul/2022:16:15:55 +0200] "GET /logo.svg HTTP/2.0" 200 836 "https://learnjsthehardway.com/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/1.1.1.1 Safari/537.36" "-" +1.1.1.1 - - [20/Jul/2022:16:15:55 +0200] "GET /images/zed.png HTTP/2.0" 200 5666 "https://learnjsthehardway.com/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/1.1.1.1 Safari/537.36" "-" +1.1.1.1 - - [20/Jul/2022:16:15:55 +0200] "GET /images/gittea.jpg HTTP/2.0" 200 102800 "https://learnjsthehardway.com/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/1.1.1.1 Safari/537.36" "-" +1.1.1.1 - - [20/Jul/2022:16:15:55 +0200] "GET /feather-sprite.svg HTTP/2.0" 200 8993 "https://learnjsthehardway.com/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/1.1.1.1 Safari/537.36" "-" +1.1.1.1 - - [20/Jul/2022:16:15:56 +0200] "GET /fonts/computer.woff HTTP/2.0" 200 9416 "https://learnjsthehardway.com/color.css" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/1.1.1.1 Safari/537.36" "-" +1.1.1.1 - - [20/Jul/2022:16:15:56 +0200] "GET /favicon.png HTTP/2.0" 200 3127 "https://learnjsthehardway.com/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/1.1.1.1 Safari/537.36" "-" +1.1.1.1 - - [20/Jul/2022:16:15:56 +0200] "GET /favicon-32x32.png HTTP/2.0" 200 505 "https://learnjsthehardway.com/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/1.1.1.1 Safari/537.36" "-" +1.1.1.1 - - [20/Jul/2022:16:15:57 +0200] "GET /site.webmanifest HTTP/2.0" 200 426 "https://learnjsthehardway.com/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/1.1.1.1 Safari/537.36" "-" diff --git a/02-filter-a-log-file/step1.js b/02-filter-a-log-file/step1.js new file mode 100644 index 0000000..9f1c1bf --- /dev/null +++ b/02-filter-a-log-file/step1.js @@ -0,0 +1,7 @@ +import fs from "fs"; + +const raw_log = fs.readFileSync(process.argv[2]); + +const lines = raw_log.toString().split("\n"); + +console.log(lines);