Skip to content

Commit

Permalink
fix: fixed tests and added readme
Browse files Browse the repository at this point in the history
  • Loading branch information
aarontravass committed Jun 26, 2023
1 parent 11725c2 commit 2956c8a
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 6 deletions.
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# deno-http-logger - A tiny, 0 Dependency http logger for Deno

## Example
```ts
const port = 5000

const app = new Server({
handler: (req) => {
const res = new Response("hello", { status: 200 });
logger({ ip: true })(req, res);
return res;
},
port: port
});

console.log(`Listening on localhost:${port}`)
await app.listenAndServe()
```
Empty file removed deps.ts
Empty file.
16 changes: 16 additions & 0 deletions example.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { Server } from "./dev_depts.ts";
import { logger } from "./mod.ts";

const port = 5000

const app = new Server({
handler: (req) => {
const res = new Response("hello", { status: 200 });
logger({ ip: true })(req, res);
return res;
},
port: port
});

console.log(`Listening on localhost:${port}`)
await app.listenAndServe()
5 changes: 3 additions & 2 deletions mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,17 @@ export interface LoggerOptions {
const generateLog =
(req: Request, res: Response, connInfo?: ConnInfo) =>
(logArray: string[], options: LoggerOptions) => {
const url = req.url;
const url = new URL(req.url);

logArray.push(
"[" + (options.output?.level || LogLevel.log).toUpperCase() + "]"
);

if (options?.ip)
logArray.push((connInfo?.remoteAddr as Deno.NetAddr)?.hostname ?? "");
logArray.push(res.status.toString());
logArray.push(req.method.toUpperCase());
logArray.push(url);
logArray.push(url.pathname);
};

export const logger = (options: LoggerOptions) => {
Expand Down
11 changes: 7 additions & 4 deletions mod_test.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
import { expect, makeFetch, describe, it, run, ConnInfo } from "./dev_depts.ts";
import { ConnInfo } from "./dev_depts.ts";
import { expect, makeFetch, describe, it, run } from "./dev_depts.ts";
import { logger } from "./mod.ts";

describe("Basic tests", () => {
it("should write logs", async () => {

const callback = (line: string) => {
expect(line).toBeDefined();
console.log(line);
console.log(line)
expect(line).toEqual('[LOG] 127.0.0.1 200 GET /');
};
const handler = (req: Request, connInfo: ConnInfo) => {
const res = new Response("hello world", { status: 200 });

logger({ output: { callback }, ip: true })(req, res, connInfo);
return res;
};

(await makeFetch(handler)("/")).expectStatus(200);
});
});
Expand Down

0 comments on commit 2956c8a

Please sign in to comment.