Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
async function listLogEntries(logName) {
// [START logging_list_log_entries]
// Imports the Google Cloud client library
const {Logging} = require('@google-cloud/logging');
// Creates a client
const logging = new Logging();
/**
* TODO(developer): Uncomment the following line to run the code.
*/
// const logName = 'Name of the log from which to list entries, e.g. my-log';
const log = logging.log(logName);
// List the most recent entries for a given log
// See
// https://googlecloudplatform.github.io/google-cloud-node/#/docs/logging/latest/logging?method=getEntries
const [entries] = await log.getEntries();
console.log('Logs:');
entries.forEach(entry => {
const metadata = entry.metadata;
console.log(`${metadata.timestamp}:`, metadata[metadata.payload]);
// This makes the logs appear in the same place as console.log()
// invocations from Cloud Functions
const METADATA = {
resource: {
type: "cloud_function",
labels: {
function_name: "CustomMetrics",
region: "us-central1"
}
},
severity: "DEBUG"
};
// The Logging instance detects the project ID from the environment
// automatically.
const logging = new Logging();
const log = logging.log(LOG_NAME);
export enum Level {
ALL = 0,
DEBUG = 1,
WARN = 2,
ERROR = 3,
NONE = 4
}
let LOG_LEVEL = Level.ALL;
export function setLogLevel(level: Level) {
LOG_LEVEL = level;
}
export function debug(message: any, ...args: any[]) {
async function listSinks() {
// [START logging_list_sinks]
// Imports the Google Cloud client library
const {Logging} = require('@google-cloud/logging');
// Creates a client
const logging = new Logging();
// See
// https://googlecloudplatform.github.io/google-cloud-node/#/docs/logging/latest/logging?method=getSinks
const [sinks] = await logging.getSinks();
console.log('Sinks:');
sinks.forEach(sink => {
console.log(sink.name);
console.log(` Destination: ${sink.metadata.destination}`);
console.log(` Filter: ${sink.metadata.filter}`);
});
// [END logging_list_sinks]
}
function getLogger() {
// Return the logger object for writing logs. Create it if necessary.
if (!loggingLog) { // eslint-disable-next-line global-require
loggingLog = require('@google-cloud/logging')(credentials)
.log(logName, { removeCircular: true }); // Mark circular refs by [Circular]
console.log('created_logger');
}
return loggingLog;
}
async function listLogEntriesAdvanced(filter, pageSize, orderBy) {
// [START logging_list_log_entries_advanced]
// Imports the Google Cloud client library
const {Logging} = require('@google-cloud/logging');
// Creates a client
const logging = new Logging();
/**
* TODO(developer): Uncomment the following lines to run the code.
*
* Filter results, e.g. "severity=ERROR"
* See https://cloud.google.com/logging/docs/view/advanced_filters for more
* filter information.
*/
// const filter = 'severity=ERROR';
// const pageSize = 5;
// const orderBy = 'timestamp';
const options = {
filter: filter,
pageSize: pageSize,
orderBy: orderBy,
async function createSink(sinkName, bucketName, filter) {
// [START logging_create_sink]
// Imports the Google Cloud client libraries
const {Logging} = require('@google-cloud/logging');
const {Storage} = require('@google-cloud/storage');
// Creates clients
const logging = new Logging();
const storage = new Storage();
/**
* TODO(developer): Uncomment the following lines to run the code.
*/
// const sinkName = 'Name of your sink, e.g. my-sink';
// const bucketName = 'Desination bucket, e.g. my-bucket';
// const filter = 'Optional log filer, e.g. severity=ERROR';
// The destination can be a Cloud Storage bucket, a Cloud Pub/Sub topic,
// or a BigQuery dataset. In this case, it is a Cloud Storage Bucket.
// See https://cloud.google.com/logging/docs/api/tasks/exporting-logs for
// information on the destination format.
const destination = storage.bucket(bucketName);
const sink = logging.sink(sinkName);
async function quickstart(
projectId = 'YOUR_PROJECT_ID', // Your Google Cloud Platform project ID
logName = 'my-log' // The name of the log to write to
) {
// Creates a client
const logging = new Logging({projectId});
// Selects the log to write to
const log = logging.log(logName);
// The data to write to the log
const text = 'Hello, world!';
// The metadata associated with the entry
const metadata = {
resource: {type: 'global'},
};
// Prepares a log entry
const entry = log.entry(metadata, text);
// Writes the log entry
async function writeLogEntry(logName) {
// [START logging_write_log_entry]
// Imports the Google Cloud client library
const {Logging} = require('@google-cloud/logging');
// Creates a client
const logging = new Logging();
/**
* TODO(developer): Uncomment the following line to run the code.
*/
// const logName = 'Name of the log to write to, e.g. my-log';
const log = logging.log(logName);
// Modify this resource to match a resource in your project
// See
// https://cloud.google.com/logging/docs/api/ref_v2beta1/rest/v2beta1/MonitoredResource
const resource = {
// This example targets the "global" resource for simplicity
type: 'global',
};
// automatically.
let emitRequestLog;
if (env !== GCPEnv.APP_ENGINE && env !== GCPEnv.CLOUD_FUNCTIONS) {
const loggingBunyanReq = new LoggingBunyan(options);
const requestLogger = bunyan.createLogger({
name: options.logName!,
streams: [loggingBunyanReq.stream(options.level as types.LogLevel)],
});
emitRequestLog = (httpRequest: HttpRequest, trace: string) => {
requestLogger.info({[LOGGING_TRACE_KEY]: trace, httpRequest});
};
}
return {
logger,
mw: commonMiddleware.express.makeMiddleware(
projectId,
makeChildLogger,
emitRequestLog
),
};
function makeChildLogger(trace: string) {
return logger.child({[LOGGING_TRACE_KEY]: trace}, true /* simple child */);
}
}
async function main() {
const logging = new Logging();
console.dir(logging);
}
main();