ts-logs
Version:
This package provide a skd for audit and manager logs in nodejs and express
173 lines • 8.78 kB
TypeScript
import { HttpConfig, Locale, LocalOpt, Logs, LProps, S3Config, Steps, LogStateType, MongoConfig } from "../types";
import { SavePayload } from "../types";
/**
* @description Global log to manage steps and behavior.
* @summary Create a global log and added steps, print or publish result.
* @summary Check `stateType` option.
* It defines the behavior of the state. Whether to change state or return a new instance without changing original state.
* If `stateType` is defined as `stateful` the original state of log will be changed. If `stateType` is defined as `stateless`
* a new instance of Log will be created and returned without change the original state.
* @default `stateType` is `stateful`
*/
export declare class Log implements Logs {
uid: string | Readonly<string>;
name: string | Readonly<string>;
ip: string | Readonly<string>;
origin: string | Readonly<string>;
createdAt: Date | Readonly<Date>;
steps: Readonly<Steps[]> | Steps[];
/**
* @description Defines the behavior of the state. Whether to change state or return a new instance without changing original state.
*/
readonly stateType: LogStateType;
private constructor();
/**
* @description Create a new global log.
* @param props as Partial LProps
* @returns instance of Log
*/
static init(props: Partial<LProps> & {
name: string;
}): Readonly<Logs> | Logs;
/**
* @description Create a new global log changing stateType.
* @param stateType as LogStateType
* @returns instance of Log
*/
clone(stateType: LogStateType): Readonly<Logs> | Logs;
/**
* @description Check if exist some step for log.
* @returns true if exist some step and return false if step is empty.
*/
hasSteps(): boolean;
/**
* @description Create a new instance of Log with ip attribute. This is an immutable instance, the method does not change state, it returns a new one.
* @param ip as request origin address
* @returns instance of Log with ip address set
* @summary Check `stateType` option.
* It defines the behavior of the state. Whether to change state or return a new instance without changing original state.
* If `stateType` is defined as `stateful` the original state of log will be changed. If `stateType` is defined as `stateless`
* a new instance of Log will be created and returned without change the original state.
* @default `stateType` is `stateful`
*/
setIp(ip: string): Readonly<Logs> | Logs;
/**
* @description Set a new uid to identify the log instance
* @param uid as unique identifier id
* @returns instance of Log with uid set
* @summary Check `stateType` option.
* It defines the behavior of the state. Whether to change state or return a new instance without changing original state.
* If `stateType` is defined as `stateful` the original state of log will be changed. If `stateType` is defined as `stateless`
* a new instance of Log will be created and returned without change the original state.
* @default `stateType` is `stateful`
*/
setId(uid: string): Readonly<Logs> | Logs;
/**
* @description Create a new instance of Log with url attribute. This is an immutable instance, the method does not change state, it returns a new one.
* @param url as request origin url address
* @returns instance of Log with url address set
* @summary Check `stateType` option.
* It defines the behavior of the state. Whether to change state or return a new instance without changing original state.
* If `stateType` is defined as `stateful` the original state of log will be changed. If `stateType` is defined as `stateless`
* a new instance of Log will be created and returned without change the original state.
* @default `stateType` is `stateful`
*/
setOrigin(url: string): Readonly<Logs> | Logs;
/**
* @description Define log name. This attribute is used to create a folder name on store local.
* @param name log name as string
* @returns instance of Log with log name set
* @summary Check `stateType` option.
* It defines the behavior of the state. Whether to change state or return a new instance without changing original state.
* If `stateType` is defined as `stateful` the original state of log will be changed. If `stateType` is defined as `stateless`
* a new instance of Log will be created and returned without change the original state.
* @default `stateType` is `stateful`
*/
setName(name: string): Readonly<Logs> | Logs;
/**
* @description Add a log step to instance.
* @param step as instance of Step.
* @returns instance of Log with added step.
* @summary Check `stateType` option.
* It defines the behavior of the state. Whether to change state or return a new instance without changing original state.
* If `stateType` is defined as `stateful` the original state of log will be changed. If `stateType` is defined as `stateless`
* a new instance of Log will be created and returned without change the original state.
* @default `stateType` is `stateful`
*/
addStep(step: Readonly<Steps>): Readonly<Logs> | Logs;
/**
* @description Add a log step to instance.
* @param step as instance of Step.
* @returns instance of Log with added step.
* @summary Check `stateType` option.
* It defines the behavior of the state. Whether to change state or return a new instance without changing original state.
* If `stateType` is defined as `stateful` the original state of log will be changed. If `stateType` is defined as `stateless`
* a new instance of Log will be created and returned without change the original state.
* @default `stateType` is `stateful`
*/
addSteps(steps: Readonly<Steps[]>): Readonly<Logs> | Logs;
/**
* @description Remove a log step from instance.
* @param uid as step uid to identify what step to remove.
* @returns instance of Log without removed step.
* @summary Check `stateType` option.
* It defines the behavior of the state. Whether to change state or return a new instance without changing original state.
* If `stateType` is defined as `stateful` the original state of log will be changed. If `stateType` is defined as `stateless`
* a new instance of Log will be created and returned without change the original state.
* @default `stateType` is `stateful`
*/
removeStep(uid: string): Readonly<Logs> | Logs;
/**
* @description Save logs locally where the app is running
* @param path absolute path of the folder where the file will be saved.
* @returns
* @emits fileName: Omit the file name as the file name is defined based on the log name plus creation date
* @example path: "/home/user/my-app/logs"
* @default path: "/[app-folder]/logs/[log-name]-[year]-[month]-[day].log".
* @throws If an error occurs in the local saving process, the logs will be displayed on the standard output (terminal)
*/
writeLocal(path?: string): Promise<void>;
/**
* @description Print log and all steps on terminal.
* @param locales as LocalesArgument to format date.
* @param options as DateTimeFormatOptions to format date.
*/
print(locales?: Locale, options?: LocalOpt): void;
/**
* @description Publish log using a provider.
* @requires provider as `S3Config` or `HttpConfig` you can provide it using CONFIG @see example below.
* @todo implement provider to publish on
* @external aws-s3
* @external http
* @example
*
* import { Config, Log } from 'ts-logs';
*
* const httpConfig = Config.Http({ url: "https://domain.com/logs" });
*
* const log = Log.init({ name: "test..." });
*
* const result = await log.publish(httpConfig);
*
* console.log(result.statusCode);
*
* > 200
*
*/
publish(config: S3Config | HttpConfig | MongoConfig): Promise<SavePayload | null>;
/**
* @description Delete expired files based in 'days' params.
* @param days as integer.
* @param dirname as string.
*/
rmLogs(days: number, dirname?: string): Promise<void>;
/**
* @description Delete all steps from log instance and generate a new uid.
* @summary If stateType is defined as `stateful` the original state will be replaced.
* @summary If stateType is defined as `stateless` a new instance will be created and the original state will not be changed.
* @returns instance of Log
*/
clear(): Logs | Readonly<Logs>;
}
export default Log;
//# sourceMappingURL=log.d.ts.map