Skip to content

Commit

Permalink
Deleted filelogger
Browse files Browse the repository at this point in the history
  • Loading branch information
Florian-Schopp committed Nov 2, 2023
1 parent db0dfe9 commit 34ef740
Show file tree
Hide file tree
Showing 2 changed files with 1 addition and 101 deletions.
32 changes: 0 additions & 32 deletions src/3_jet/log.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import * as fs from 'fs'
export enum LogLevel {
'socket' = 1,
'debug',
Expand All @@ -22,7 +21,6 @@ export class Logger {
logName: string
logLevel: LogLevel
callBacks: LogFunction[] | undefined
stream: fs.WriteStream | undefined
/**
* Constructor to create a new Logger instance
* @param settings
Expand All @@ -31,9 +29,6 @@ export class Logger {
this.logName = settings.logName
this.logLevel = settings.logLevel || LogLevel['none']
this.callBacks = settings.logCallbacks
if (settings.logFile) {
this.stream = fs.createWriteStream(settings.logFile)
}
}
/**
* Function that transforms a message into a string of the format "<Date> <Time> <LogName> <LogLevel> <Message>"
Expand All @@ -58,10 +53,6 @@ export class Logger {
return
}
const logMessage = this.stringBuilder(msg, level)
if (this.stream) {
this.stream.write(logMessage)
this.stream.write('\n')
}
if (this.callBacks) {
this.callBacks.every((cb) => cb(logMessage))
}
Expand Down Expand Up @@ -101,27 +92,4 @@ export class Logger {
error(msg: string) {
this.log(msg, LogLevel.error)
}
/**
* Function that can be called to wait until the stream has completed to write everything
*/
flush(): Promise<void> {
return new Promise((resolve) => {
if (this.stream) {
const interval = setInterval(() => {
if (!this.stream || !this.stream.pending) {
clearInterval(interval)
resolve()
}
})
}
})
}
/**
* Function to close the Logger and the fileStream in case any fileStream was used
*/
close() {
if (this.stream) {
this.stream.end()
}
}
}
70 changes: 1 addition & 69 deletions test/log.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Logger, LogLevel } from '../src/3_jet/log'
import { readFileSync, mkdirSync, rmSync, existsSync } from 'fs'
import { mkdirSync, rmSync, existsSync } from 'fs'
const tmpDir = '.cache/tmp'
describe('Testing Logging', () => {
beforeAll(() => {
Expand Down Expand Up @@ -27,7 +27,6 @@ describe('Testing Logging', () => {
logger.info('Bar')
logger.warn('Bar')
logger.error('Bar')
return logger.flush().then(() => logger.close())
}
describe('Should test callbacks', () => {
it('Should create empty logger', () => {
Expand Down Expand Up @@ -85,71 +84,4 @@ describe('Testing Logging', () => {
expect(logSpy).toHaveBeenCalledTimes(2)
})
})
describe('Should test file', () => {
it('Should create sock logger', async () => {
const logFile = `${tmpDir}/sock.txt`
const logger = new Logger({
logLevel: LogLevel.socket,
logName: 'Foo',
logFile
})
await log(logger).then(() => {
const content = readFileSync(logFile).toString().split('\n')
expect(content).toHaveLength(12)
expect(content[0]).toContain('Foo socket Bar')
})
})
it('Should create debug logger', async () => {
const logFile = `${tmpDir}/debug.txt`
const logger = new Logger({
logLevel: LogLevel.debug,
logName: 'Foo',
logFile
})
await log(logger).then(() => {
const content = readFileSync(logFile).toString().split('\n')
expect(content).toHaveLength(10)
expect(content[0]).toContain('Foo debug Bar')
})
})
it('Should create info logger', async () => {
const logFile = `${tmpDir}/info.txt`
const logger = new Logger({
logLevel: LogLevel.info,
logName: 'Foo',
logFile
})
await log(logger).then(() => {
const content = readFileSync(logFile).toString().split('\n')
expect(content).toHaveLength(7)
expect(content[0]).toContain('Foo info Bar')
})
})
it('Should create warn logger', async () => {
const logFile = `${tmpDir}/warn.txt'`
const logger = new Logger({
logLevel: LogLevel.warn,
logName: 'Foo',
logFile
})
await log(logger).then(() => {
const content = readFileSync(logFile).toString().split('\n')
expect(content).toHaveLength(5)
expect(content[0]).toContain('Foo warn Bar')
})
})
it('Should create error logger', async () => {
const logFile = `${tmpDir}/error.txt'`
const logger = new Logger({
logLevel: LogLevel.error,
logName: 'Foo',
logFile
})
await log(logger).then(() => {
const content = readFileSync(logFile).toString().split('\n')
expect(content).toHaveLength(3)
expect(content[0]).toContain('Foo error Bar')
})
})
})
})

0 comments on commit 34ef740

Please sign in to comment.