Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add getDateString config #104

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/FileStreamRotator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export default class FileStreamRotator extends EventEmitter {
}
config.auditSettings = auditSettings

config.rotationSettings = DefaultOptions.rotationSettings({filename: options.filename, extension: options.extension})
config.rotationSettings = DefaultOptions.rotationSettings({ filename: options.filename, extension: options.extension })
if (options.date_format && !options.frequency){
config.rotationSettings.frequency = Frequency.date
} else {
Expand All @@ -78,6 +78,9 @@ export default class FileStreamRotator extends EventEmitter {
if (options.date_format) {
config.rotationSettings.format = options.date_format
}
if (typeof options.getDateString === 'function') {
config.rotationSettings.getDateString = options.getDateString
}
config.rotationSettings.utc = options.utc ?? false
switch(options.frequency){
case "daily":
Expand Down
4 changes: 4 additions & 0 deletions src/Rotator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,10 @@ export default class Rotator {
getDateString(date?: Date): string {
let _date: Date = date || new Date()

if (typeof this.settings.getDateString === 'function') {
return this.settings.getDateString(_date);
}

let components: DateComponents = Rotator.getDateComponents(_date, this.settings.utc)

let format = this.settings.format
Expand Down
4 changes: 4 additions & 0 deletions src/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export type RotationSettings = {
format?: string
utc: boolean
extension?: string
getDateString(date?: Date): string
}

export type FileOptions = {
Expand Down Expand Up @@ -89,6 +90,9 @@ export type FileStreamRotatorOptions = {
symlink_name?: string
/** Use specified hashing algorithm for audit. Defaults to 'md5'. Use 'sha256' for FIPS compliance. */
audit_hash_type?: "md5" | "sha256"

/** use this to define your filename with date */
getDateString?: (date?: Date) => string
}

export type FileStreamRotatorConfig = {
Expand Down