diff --git a/src/index.ts b/src/index.ts index b41c9f82..3d08bc1f 100644 --- a/src/index.ts +++ b/src/index.ts @@ -552,12 +552,20 @@ class Logging { opts?: GetEntriesRequest | GetEntriesCallback ): Promise { const options = opts ? (opts as GetEntriesRequest) : {}; - const reqOpts = extend( - { - orderBy: 'timestamp desc', - }, - options - ); + + // By default, sort entries by descending timestamp + let reqOpts = extend({orderBy: 'timestamp desc'}, options); + + // By default, filter entries to last 24 hours only + const time = new Date(); + time.setDate(time.getDate() - 1); + const timeFilter = `timestamp >= "${time.toISOString()}"`; + if (!options.filter) { + reqOpts = extend({filter: timeFilter}, reqOpts); + } else if (!options.filter.includes('timestamp')) { + reqOpts.filter += ` AND ${timeFilter}`; + } + reqOpts.resourceNames = arrify(reqOpts.resourceNames!); this.projectId = await this.auth.getProjectId(); const resourceName = 'projects/' + this.projectId; diff --git a/test/index.ts b/test/index.ts index edd7bf43..fce53c12 100644 --- a/test/index.ts +++ b/test/index.ts @@ -477,26 +477,29 @@ describe('Logging', () => { logging.auth.getProjectId = async () => PROJECT_ID; }); - it('should exec without options', async () => { + it('should exec without options (with defaults)', async () => { logging.loggingService.listLogEntries = async ( - reqOpts: {}, + // eslint-disable-next-line @typescript-eslint/no-explicit-any + reqOpts: any, gaxOpts: {} ) => { assert.deepStrictEqual(reqOpts, { + filter: reqOpts?.filter, orderBy: 'timestamp desc', resourceNames: ['projects/' + logging.projectId], }); assert.deepStrictEqual(gaxOpts, { autoPaginate: undefined, }); + assert.ok(reqOpts?.filter.includes('timestamp')); return [[]]; }; await logging.getEntries(); }); - it('should accept options', async () => { - const options = {filter: 'test'}; + it('should accept options (and not overwrite timestamp)', async () => { + const options = {filter: 'timestamp > "2020-11-11T15:01:23.045123456Z"'}; logging.loggingService.listLogEntries = async ( reqOpts: {}, @@ -505,7 +508,7 @@ describe('Logging', () => { assert.deepStrictEqual( reqOpts, extend(options, { - filter: 'test', + filter: 'timestamp > "2020-11-11T15:01:23.045123456Z"', orderBy: 'timestamp desc', resourceNames: ['projects/' + logging.projectId], }) @@ -520,6 +523,32 @@ describe('Logging', () => { await logging.getEntries(options); }); + it('should append default timestamp to existing filters', async () => { + const options = {filter: 'test'}; + + logging.loggingService.listLogEntries = async ( + // eslint-disable-next-line @typescript-eslint/no-explicit-any + reqOpts: any, + gaxOpts: {} + ) => { + assert.deepStrictEqual( + reqOpts, + extend(options, { + filter: reqOpts?.filter, + orderBy: 'timestamp desc', + resourceNames: ['projects/' + logging.projectId], + }) + ); + assert.deepStrictEqual(gaxOpts, { + autoPaginate: undefined, + }); + assert.ok(reqOpts?.filter.includes('test AND timestamp')); + return [[]]; + }; + + await logging.getEntries(options); + }); + it('should not push the same resourceName again', async () => { const options = { resourceNames: ['projects/' + logging.projectId], @@ -567,12 +596,14 @@ describe('Logging', () => { assert.deepStrictEqual(reqOpts, { a: 'b', c: 'd', + filter: reqOpts?.filter, orderBy: 'timestamp desc', resourceNames: ['projects/' + logging.projectId], }); // eslint-disable-next-line @typescript-eslint/no-explicit-any assert.strictEqual((reqOpts as any).gaxOptions, undefined); assert.deepStrictEqual(gaxOpts, options.gaxOptions); + assert.ok(reqOpts?.filter.includes('timestamp')); return [[]]; };