Giter Club home page Giter Club logo

Comments (8)

crookse avatar crookse commented on May 23, 2024 1

@code0x9 , i do agree that a skip feature would help and bloating the logs degrades developer ux when you're debugging and stuff. however, a lot of the stuff in drash land is made to be extensible which is why a lot of the modules are class-based. i know you don't want to go the inheritance way, but unfortunately there's no other way to accomplish what you want unless:

  1. you go the inheritance way; or
  2. @Guergeiro, @ebebbington and i discuss adding the new feature and getting it released (which could take time and we don't want to be a blocker in your development efforts)

@Guergeiro provided the resource inheritance way, but there's also another way: class MyService extends DexterService { ... }. this way it keeps you from having to modify resources and extending resources in case that's not your jam. here's a simple example (and you can copy-paste this code and it should work when you run it):

import * as Drash from "https://deno.land/x/[email protected]/mod.ts";
import { DexterService } from "https://deno.land/x/[email protected]/src/services/dexter/dexter.ts";

/**
 * Class to override DexterService that can skip the logging process for
 * specific requests.
 */
class SkippableDexterService extends DexterService {
  /**
   * Write logs after a resource is hit.
   * @param request
   * @param response
   */
  runAfterResource(request: Drash.Request, response: Drash.Response): void {
    if (this.#skip(request)) {
      return;
    }
    super.runAfterResource(request, response);
  }

  /**
   * Write logs before a resource is hit.
   * @param request
   * @param response
   */
  public runBeforeResource(
    request: Drash.Request,
    response: Drash.Response,
  ): void {
    if (this.#skip(request)) {
      return;
    }
    super.runBeforeResource(request, response);
  }

  /**
   * Should logging be skipped for the request in question?
   * @param request
   * @returns True if yes, false if no.
   */
  #skip(request: Drash.Request): boolean {
    const url = new URL(request.url);
    if (url.pathname === "/health_check") {
      return true;
    }
    return false;
  }
}

// Create the skippable version of DexterService to be plugged into the server
const dexter = new SkippableDexterService({
  enabled: true,
  method: true,
  url: true,
});

/**
 * Some basic resource.
 */
class HomeResource extends Drash.Resource {
  public paths = ["/", "/home"];

  public GET(request: Drash.Request, response: Drash.Response): void {
    return response.text("Hello");
  }
}

/**
 * Some resource that has a path that matches SkippableDexterService#skip list.
 */
class SkipsDexterServiceResource extends Drash.Resource {
  public paths = ["/health_check"];

  public GET(request: Drash.Request, response: Drash.Response): void {
    return response.text("Hello");
  }
}

// Create your server and plug in the skippable dexter service
const server = new Drash.Server({
  resources: [
    HomeResource,
    SkipsDexterServiceResource,
  ],
  services: [
    dexter,
  ],
  hostname: "0.0.0.0",
  port: 1447,
  protocol: "http",
});

server.run();

console.log(`Server running at ${server.address}`);
console.log(`Try it out:\n  - /home will result in logs\n  - /health_check will skip logging`);

from drash.

code0x9 avatar code0x9 commented on May 23, 2024 1

@crookse Thanks for your explanation and sample code. I'll try to go with inheritance as you recommended.

from drash.

Guergeiro avatar Guergeiro commented on May 23, 2024

Can't you implement Dexter only for the resources you want with resource level services? That is, if you don't want Dexter on /health_check, add Dexter to all other resources except /health_check.

from drash.

code0x9 avatar code0x9 commented on May 23, 2024

of course your suggestion will work. I just wanted to enable dexter on all resources except some special endpoints.
for example, I want to run drash on K8S and with proves. K8S will keep calling prove endpoints and I don't want K8S to fill up my log storage.
by skip feature, I can add dexter by server-level to reduce duplicate resource-level dexter service configurations.

from drash.

Guergeiro avatar Guergeiro commented on May 23, 2024

If you want to add to Dexter to all resources more automatically:

export abstract class MyBaseResourceWithBetterName extends Drash.Resource {
  public services = {
    ALL: [
      new DexterService(),
    ],
  };
}

class HomeResource extends MyBaseResourceWithBetterName {
  public paths = ["/home", "/foo"];
  // Uses dexter
}

class HealthCheck extends Drash.Resource {
  public paths = ["/health_check"];
  // Doesn't use dexter
}

Will this solve your issue? The problem here is bloating the service with extra config props that are not really required. If we accept this one, more will follow.

from drash.

code0x9 avatar code0x9 commented on May 23, 2024

skip feature is just a utility function which can solve some problems without class inheritance or code duplicates. morgan provides this(https://github.com/expressjs/morgan#skip) and it's quite useful.

And personally, I prefer Composition over inheritance principle so I'd like to solve this by non-inheritance way.

There can be more scenario like "I want to skip log from internal IP" or "I want to skip log when response code is below 400" which skip feature can help.

from drash.

ebebbington avatar ebebbington commented on May 23, 2024

@code0x9 feel free to let us know how you get on!

from drash.

crookse avatar crookse commented on May 23, 2024

closing since there hasn't been activity on this for a while. anyone feel free to open for discussion again!

from drash.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.