Giter VIP home page Giter VIP logo

Comments (4)

pimmee avatar pimmee commented on May 29, 2024 1

Any progress on this @adikus?

from logtail-js.

adikus avatar adikus commented on May 29, 2024

Thanks @pimmee for the suggestions. Most of these make sense to me, I'll make a ticket in our internal system so that we can get back to this.

There's couple of things though:

  • I'm not sure if we'd want to support unknown types in the library, I would prefer we left that to the user to cast it to Error first (but we should defo support Error).

  • In your Stackblitz example I noticed that you're doing:

    interface Payload2 {
      a: string;
      b?: string;
    }
    const payload2: Payload2 = { a: 'a', b: 'b' };
    logger.info('not working', payload2);

    One of the following instead should work:

    type Payload2 = Record<'a'|'b', string>;
    const payload2: Payload2 = { a: 'a', b: 'b' };
    logger.info('not working', payload2);
    interface Payload2 { [key: string]: string; }
    const payload2: Payload2 = { a: 'a', b: 'b' };
    logger.info('not working', payload2);

    The problem with the original was that typescript couldn't determine the type of the index, the Context type needs it to be defined as string. I realize that these are not the same though and I'm not sure if there's another way to mark the index as string in Typescript while preserving the original interface 😕 .

from logtail-js.

pimmee avatar pimmee commented on May 29, 2024

Thank you for the quick follow up.

  • I do think it improves the DX greatly if logtail could handle unknown internally, so that we (the users) wouldn't have to do err as Error or if (err instanceof Error) everywhere where we catch an error and want to log it.

Also, a lot of projects restricts type assertion with eslint, and we'd have to add disable eslint comments everywhere. This makes it rather cluttered IMO.

try {
   ...
} catch (err) {
   // eslint-disable-next-line @typescript-eslint/consistent-type-assertions
   logger.error(err as Error);
}

In the code within logtail where you sanitize the log data, you could do something like:

if (value instanceof Error) {
   return value.message; // potentially do something with value.stack to log error stack trace?
}

For other unknown values that logtail is unable to parse, I'd say they can be ignored as it's unlikely that we'd be able to do anything with it or be interested anyway.

Maybe even typing Context as Record<string, unknown> with logtail parsing/sanitizing everything that it can could work? So basically extending the parsing to handle arrays, etc., so that we can log anything with logtail doing it's best effort to handle it

  • I do know that there are other ways of typing objects in Typescript so that they can be logged with logtail. However, a lot of Typescript projects use undefined extensively on their interfaces and types. This makes it cumbersome to work with logtail.

Typing an object like the following is a very common way in Typescript for defining optional fields:

interface Address {
     id: string;
     town: string;
     country: string;
     postal_code: string;
     state?: string;
     floor?: number;
}

Not being able to log these objects is a showstopper, so I definitely think it should be supported.

In one project I work on we use the following code snippet, which could perhaps be of inspiration:

// https://github.com/apollographql/apollo-client/issues/2412#issuecomment-755449680
type RecursivelyReplaceNullWithUndefined<T> = T extends null
  ? undefined
  : {
      [K in keyof T]: T[K] extends (infer U)[]
        ? RecursivelyReplaceNullWithUndefined<U>[]
        : RecursivelyReplaceNullWithUndefined<T[K]>;
    };
/**
 * Recursively removes all null and undefined properties of an object
 * @param input Some object
 * @returns Cleaned object (without null and undefined properties)
 */
export function omitNullishValues<T extends object>(
  input: T
): RecursivelyReplaceNullWithUndefined<T> {
  // See https://stackoverflow.com/questions/37246775/
  const omitByRecursively = (value: object, iteratee: (value: string, key: string) => boolean) => {
    const cb = (v: Record<string, unknown>): unknown => omitByRecursively(v, iteratee);
    return value.constructor.name === 'Object' || value.constructor.name === 'Array'
      ? lodash.isArray(value)
        ? lodash.map(value, cb)
        : lodash(value).omitBy(iteratee).mapValues(cb).value()
      : value;
  };
  // eslint-disable-next-line @typescript-eslint/consistent-type-assertions
  return omitByRecursively(
    input,
    value => value === null || value === undefined
  ) as RecursivelyReplaceNullWithUndefined<T>;
}

Let me know if there's anything I can do to help.

from logtail-js.

PetrHeinz avatar PetrHeinz commented on May 29, 2024

Hi @pimme,

thanks for opening the issue and providing the details. We originally thought strongly typed context would help devs to know what kinds of types can be safely used, but the added complexity of use is really not worth it.

Since v0.4.6 the work with context is much more streamlined 👍 The only difference from your demo is that passing err: unknown directly as log message is not allowed. You can pass it as a context though (logger.error("Flux capacitor malfunctioned", err)).

I've also improved working with errors and unserializable types (eg. functions), and same process is used in Browser logging, if you're using it 🙂

Thank you for your patience 🙏 Feel free to reopen the issue if something feels off or you have any question.

from logtail-js.

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.