index.d.ts 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. // Type definitions for logform 2.x
  2. // Project: https://github.com/winstonjs/logform
  3. // Definitions by: DABH <https://github.com/DABH>
  4. // Definitions: https://github.com/winstonjs/logform
  5. // TypeScript Version: 2.2
  6. import {LEVEL, MESSAGE, SPLAT} from 'triple-beam'
  7. export interface TransformableInfo {
  8. level: string;
  9. message: any;
  10. [LEVEL]?: string;
  11. [MESSAGE]?: any;
  12. [SPLAT]?: any;
  13. [key: string | symbol]: any;
  14. }
  15. export type TransformFunction = (info: TransformableInfo, opts?: any) => TransformableInfo | boolean;
  16. export type Colors = { [key: string]: string | string[] }; // tslint:disable-line interface-over-type-literal
  17. export type FormatWrap = (opts?: any) => Format;
  18. export class Format {
  19. constructor(opts?: object);
  20. options?: object;
  21. transform: TransformFunction;
  22. }
  23. export class Colorizer extends Format {
  24. constructor(opts?: object);
  25. createColorize: (opts?: object) => Colorizer;
  26. addColors: (colors: Colors) => Colors;
  27. colorize: (level: string, message: string) => string;
  28. }
  29. export function format(transform: TransformFunction): FormatWrap;
  30. export function levels(config: object): object;
  31. export namespace format {
  32. function align(): Format;
  33. function cli(opts?: CliOptions): Format;
  34. function colorize(opts?: ColorizeOptions): Colorizer;
  35. function combine(...formats: Format[]): Format;
  36. function errors(opts?: object): Format;
  37. function json(opts?: JsonOptions): Format;
  38. function label(opts?: LabelOptions): Format;
  39. function logstash(): Format;
  40. function metadata(opts?: MetadataOptions): Format;
  41. function ms(): Format;
  42. function padLevels(opts?: PadLevelsOptions): Format;
  43. function prettyPrint(opts?: PrettyPrintOptions): Format;
  44. function printf(templateFunction: (info: TransformableInfo) => string): Format;
  45. function simple(): Format;
  46. function splat(): Format;
  47. function timestamp(opts?: TimestampOptions): Format;
  48. function uncolorize(opts?: UncolorizeOptions): Format;
  49. }
  50. export interface CliOptions extends ColorizeOptions, PadLevelsOptions { }
  51. export interface ColorizeOptions {
  52. /**
  53. * If set to `true` the color will be applied to the `level`.
  54. */
  55. level?: boolean;
  56. /**
  57. * If set to `true` the color will be applied to the `message` and `level`.
  58. */
  59. all?: boolean;
  60. /**
  61. * If set to `true` the color will be applied to the `message`.
  62. */
  63. message?: boolean;
  64. /**
  65. * An object containing the colors for the log levels. For example: `{ info: 'blue', error: 'red' }`.
  66. */
  67. colors?: Record<string, string>;
  68. }
  69. export interface JsonOptions {
  70. /**
  71. * A function that influences how the `info` is stringified.
  72. */
  73. replacer?: (this: any, key: string, value: any) => any;
  74. /**
  75. * The number of white space used to format the json.
  76. */
  77. space?: number;
  78. // The following options come from safe-stable-stringify
  79. // https://github.com/BridgeAR/safe-stable-stringify/blob/main/index.d.ts
  80. /**
  81. * If `true`, bigint values are converted to a number. Otherwise, they are ignored.
  82. * This option is ignored by default as Logform stringifies BigInt in the default replacer.
  83. * @default true
  84. */
  85. bigint?: boolean,
  86. /**
  87. * Defines the value for circular references.
  88. * Set to `undefined`, circular properties are not serialized (array entries are replaced with null).
  89. * Set to `Error`, to throw on circular references.
  90. * @default "[Circular]"
  91. */
  92. circularValue?: string | null | TypeErrorConstructor | ErrorConstructor,
  93. /**
  94. * If `true`, guarantee a deterministic key order instead of relying on the insertion order.
  95. * @default true
  96. */
  97. deterministic?: boolean,
  98. /**
  99. * Maximum number of entries to serialize per object (at least one).
  100. * The serialized output contains information about how many entries have not been serialized.
  101. * Ignored properties are counted as well (e.g., properties with symbol values).
  102. * Using the array replacer overrules this option.
  103. * @default Infinity
  104. */
  105. maximumBreadth?: number,
  106. /**
  107. * Maximum number of object nesting levels (at least 1) that will be serialized.
  108. * Objects at the maximum level are serialized as `"[Object]"` and arrays as `"[Array]"`.
  109. * @default Infinity
  110. */
  111. maximumDepth?: number,
  112. }
  113. export interface LabelOptions {
  114. /**
  115. * A label to be added before the message.
  116. */
  117. label?: string;
  118. /**
  119. * If set to `true` the `label` will be added to `info.message`. If set to `false` the `label`
  120. * will be added as `info.label`.
  121. */
  122. message?: boolean;
  123. }
  124. export interface MetadataOptions {
  125. /**
  126. * The name of the key used for the metadata object. Defaults to `metadata`.
  127. */
  128. key?: string;
  129. /**
  130. * An array of keys that should not be added to the metadata object.
  131. */
  132. fillExcept?: string[];
  133. /**
  134. * An array of keys that will be added to the metadata object.
  135. */
  136. fillWith?: string[];
  137. }
  138. export interface PadLevelsOptions {
  139. /**
  140. * Log levels. Defaults to `configs.npm.levels` from [triple-beam](https://github.com/winstonjs/triple-beam)
  141. * module.
  142. */
  143. levels?: Record<string, number>;
  144. }
  145. export interface PrettyPrintOptions {
  146. /**
  147. * A `number` that specifies the maximum depth of the `info` object being stringified by
  148. * `util.inspect`. Defaults to `2`.
  149. */
  150. depth?: number;
  151. /**
  152. * Colorizes the message if set to `true`. Defaults to `false`.
  153. */
  154. colorize?: boolean;
  155. }
  156. export interface TimestampOptions {
  157. /**
  158. * Either the format as a string accepted by the [fecha](https://github.com/taylorhakes/fecha)
  159. * module or a function that returns a formatted date. If no format is provided `new
  160. * Date().toISOString()` will be used.
  161. */
  162. format?: string | (() => string);
  163. /**
  164. * The name of an alias for the timestamp property, that will be added to the `info` object.
  165. */
  166. alias?: string;
  167. }
  168. export interface UncolorizeOptions {
  169. /**
  170. * Disables the uncolorize format for `info.level` if set to `false`.
  171. */
  172. level?: boolean;
  173. /**
  174. * Disables the uncolorize format for `info.message` if set to `false`.
  175. */
  176. message?: boolean;
  177. /**
  178. * Disables the uncolorize format for `info[MESSAGE]` if set to `false`.
  179. */
  180. raw?: boolean;
  181. }