Skip to content
This repository was archived by the owner on Mar 11, 2026. It is now read-only.
Merged
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
3 changes: 0 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,9 @@
.coverage
.nyc_output
docs/
out/
build/
system-test/secrets.js
system-test/*key.json
*.lock
.DS_Store
google-cloud-logging-winston-*.tgz
google-cloud-logging-bunyan-*.tgz
package-lock.json
11 changes: 5 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,9 @@
"repository": "googleapis/nodejs-logging",
"main": "./build/src/index.js",
"files": [
"build",
"AUTHORS",
"CONTRIBUTORS",
"LICENSE"
"build/src",
Comment thread
JustinBeckwith marked this conversation as resolved.
"build/protos",
"build/proto"
],
"keywords": [
"google apis client",
Expand Down Expand Up @@ -41,13 +40,13 @@
"lint": "gts check && eslint '**/*.js'",
"check": "gts check",
"clean": "gts clean",
"compile": "tsc -p . && cp -r src/v2 build/src/v2 && cp -r protos build && cp test/*.js build/test",
"compile": "tsc -p . && cp -r src/v2 build/src/v2 && cp -r protos build && cp -r proto build && cp test/*.js build/test",
"fix": "gts fix && eslint --fix '**/*.js'",
"prepare": "npm run compile",
"pretest": "npm run compile",
"posttest": "npm run check",
"proto": "npm run proto:logging",
"proto:logging": "mkdir -p proto && pbjs -t static-module -w commonjs -p node_modules/google-proto-files -o proto/logging.js google/logging/v2/logging.proto && pbts -o proto/logging.d.ts proto/logging.js"
"proto:logging": "mkdir -p proto && pbjs -t static-module -w commonjs -p node_modules/google-proto-files google/logging/v2/logging.proto | pbts -o proto/logging.d.ts -"
},
"dependencies": {
"@google-cloud/common-grpc": "^0.9.2",
Expand Down
6,371 changes: 6,371 additions & 0 deletions proto/logging.d.ts

Large diffs are not rendered by default.

40 changes: 29 additions & 11 deletions src/entry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,24 @@ import {Service} from '@google-cloud/common-grpc';
const EventId = require('eventid');
import * as extend from 'extend';
import * as is from 'is';
import {google} from '../proto/logging';

const eventId = new EventId();

export type LogEntry = google.logging.v2.ILogEntry;
export type Timestamp = google.protobuf.IDuration;

export interface EntryJson {
timestamp: Timestamp|Date;
insertId: number;
jsonPayload?: google.protobuf.IStruct;
textPayload?: string;
}

export interface ToJsonOptions {
removeCircular?: boolean;
}

/**
* Create an entry object to define new data to insert into a log.
*
Expand Down Expand Up @@ -81,9 +96,11 @@ const eventId = new EventId();
* });
*/
class Entry {
metadata;
data;
constructor(metadata?, data?) {
metadata: LogEntry;
// tslint:disable-next-line no-any
data: any;
// tslint:disable-next-line no-any
constructor(metadata?: LogEntry, data?: any) {
/**
* @name Entry#metadata
* @type {object}
Expand Down Expand Up @@ -119,9 +136,9 @@ class Entry {
* @param {boolean} [options.removeCircular] Replace circular references in an
* object with a string value, `[Circular]`.
*/
toJSON(options) {
toJSON(options: ToJsonOptions) {
options = options || {};
const entry = extend(true, {}, this.metadata);
const entry = extend(true, {}, this.metadata) as {} as EntryJson;
if (is.object(this.data)) {
// tslint:disable-next-line no-any
entry.jsonPayload = (Service as any).objToStruct_(this.data, {
Expand All @@ -132,7 +149,7 @@ class Entry {
entry.textPayload = this.data;
}
if (is.date(entry.timestamp)) {
const seconds = entry.timestamp.getTime() / 1000;
const seconds = (entry.timestamp as Date).getTime() / 1000;
const secondsRounded = Math.floor(seconds);
entry.timestamp = {
seconds: secondsRounded,
Expand All @@ -151,17 +168,18 @@ class Entry {
* [LogEntry](https://cloud.google.com/logging/docs/reference/v2/rest/v2/LogEntry).
* @returns {Entry}
*/
static fromApiResponse_(entry) {
let data = entry[entry.payload];
static fromApiResponse_(entry: google.logging.v2.LogEntry) {
let data = entry[entry.payload!];
if (entry.payload === 'jsonPayload') {
// tslint:disable-next-line no-any
data = (Service as any).structToObj_(data);
}
const serializedEntry = new Entry(entry, data);
if (serializedEntry.metadata.timestamp) {
let ms = serializedEntry.metadata.timestamp.seconds * 1000;
ms += serializedEntry.metadata.timestamp.nanos / 1e6;
serializedEntry.metadata.timestamp = new Date(ms);
let ms = Number(serializedEntry.metadata.timestamp.seconds) * 1000;
ms += Number(serializedEntry.metadata.timestamp.nanos) / 1e6;
// tslint:disable-next-line no-any
(serializedEntry as any).metadata.timestamp = new Date(ms);
}
return serializedEntry;
}
Expand Down
48 changes: 28 additions & 20 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {promisifyAll} from '@google-cloud/promisify';
import * as arrify from 'arrify';
import * as extend from 'extend';
import {GoogleAuth} from 'google-auth-library';
import * as gax from 'google-gax';
import * as is from 'is';

const pumpify = require('pumpify');
Expand All @@ -36,8 +37,15 @@ const PKG = require('../../package.json');
const v2 = require('./v2');

import {Entry} from './entry';
import {Log} from './log';
import {Log, GetEntriesRequest} from './log';
import {Sink} from './sink';
import {Duplex} from 'stream';
import {AbortableDuplex} from '@google-cloud/common';

export interface LoggingOptions extends gax.GoogleAuthOptions {
autoRetry?: boolean;
maxRetries?: number;
}

/**
* @namespace google
Expand Down Expand Up @@ -115,12 +123,12 @@ import {Sink} from './sink';
* Full quickstart example:
*/
class Logging {
api;
api: {};
auth: GoogleAuth;
options;
options: LoggingOptions;
projectId: string;

constructor(options?) {
constructor(options?: LoggingOptions) {
// Determine what scopes are needed.
// It is the union of the scopes on all three clients.
const scopes: Array<{}> = [];
Expand Down Expand Up @@ -227,7 +235,7 @@ class Logging {
* region_tag:logging_create_sink
* Another example:
*/
createSink(name, config, callback) {
createSink(name: string, config, callback) {
// jscs:enable maximumLineLength
const self = this;
if (!is.string(name)) {
Expand Down Expand Up @@ -473,22 +481,22 @@ class Logging {
* this.end();
* });
*/
getEntriesStream(options) {
getEntriesStream(options: GetEntriesRequest) {
const self = this;
options = options || {};
let requestStream;
const userStream = streamEvents(pumpify.obj());
// tslint:disable-next-line no-any
(userStream as any).abort = () => {
let requestStream: Duplex;
const userStream = streamEvents<Duplex>(pumpify.obj());
(userStream as AbortableDuplex).abort = () => {
if (requestStream) {
requestStream.abort();
(requestStream as AbortableDuplex).abort();
}
};
const toEntryStream = through.obj((entry, _, next) => {
next(null, Entry.fromApiResponse_(entry));
});
userStream.once('reading', () => {
const reqOpts = extend(
// tslint:disable-next-line no-any
const reqOpts: any = extend(
{
orderBy: 'timestamp desc',
},
Expand Down Expand Up @@ -706,7 +714,7 @@ class Logging {
* const logging = new Logging();
* const sink = logging.sink('my-sink');
*/
sink(name) {
sink(name: string) {
return new Sink(this, name);
}

Expand All @@ -724,10 +732,10 @@ class Logging {
const self = this;
const isStreamMode = !callback;
let gaxStream;
let stream;
let stream: Duplex;
if (isStreamMode) {
stream = streamEvents(through.obj());
stream.abort = () => {
stream = streamEvents<Duplex>(through.obj());
(stream as AbortableDuplex).abort = () => {
if (gaxStream && gaxStream.cancel) {
gaxStream.cancel();
}
Expand Down Expand Up @@ -789,7 +797,7 @@ class Logging {
});
return;
}
return stream;
return stream!;
}

/**
Expand All @@ -801,7 +809,7 @@ class Logging {
*
* @private
*/
setAclForBucket_(name, config, callback) {
setAclForBucket_(name: string, config, callback) {
const self = this;
const bucket = config.destination;
bucket.acl.owners.addGroup('cloud-logs@google.com', (err, apiResp) => {
Expand All @@ -823,7 +831,7 @@ class Logging {
*
* @private
*/
setAclForDataset_(name, config, callback) {
setAclForDataset_(name: string, config, callback) {
const self = this;
const dataset = config.destination;
dataset.getMetadata((err, metadata, apiResp) => {
Expand Down Expand Up @@ -864,7 +872,7 @@ class Logging {
*
* @private
*/
setAclForTopic_(name, config, callback) {
setAclForTopic_(name: string, config, callback) {
const self = this;
const topic = config.destination;
topic.iam.getPolicy((err, policy, apiResp) => {
Expand Down
Loading