A logSync is a named collection of entries in structured log format. In Cloud Logging, structured logs refer to log entries that use the jsonPayload field to add structure to their payloads. In most GCP environments, like GKE and Cloud Functions, structured logs written to process.stdout are automatically picked up and formatted by logging agents.
Recommended for Serverless environment logging, especially where async log calls made by the Log
class can be dropped by the CPU.
Package
@google-cloud/loggingExample
const
{
Logging
}
=
require
(
' @google-cloud/logging
'
);
const
logging
=
new
Logging
();
const
log
=
logging
.
logSync
(
'mylog'
);
Constructors
(constructor)(logging, name, transport)
constructor
(
logging
:
Logging
,
name
:
string
,
transport
?:
Writable
);
Constructs a new instance of the LogSync
class
Name | Description |
---|---|
logging | Logging
|
name | string
|
transport | Writable
|
Properties
formattedName_
formattedName_
:
string
;
logging
logging
:
Logging
;
name
name
:
string
;
transport
transport
:
Writable
;
Methods
alert(entry, options)
alert
(
entry
:
Entry
|
Entry
[],
options
?:
WriteOptions
)
:
void
;
Write a log entry with a severity of "ALERT".
This is a simple wrapper around . All arguments are the same as documented there.
Name | Description |
entry | Entry
| Entry
[]
A log entry, or array of entries, to write. |
options | WriteOptions
Write options |
Type | Description |
---|---|
void |
const
{
Logging
}
=
require
(
' @google-cloud/logging
'
);
const
logging
=
new
Logging
();
const
log
=
logging
.
logSync
(
'my-log'
);
const
entry
=
log
.
entry
(
'gce_instance'
,
{
instance
:
'my_instance'
});
log
.
alert
(
entry
);
critical(entry, options)
critical
(
entry
:
Entry
|
Entry
[],
options
?:
WriteOptions
)
:
void
;
Write a log entry with a severity of "CRITICAL".
This is a simple wrapper around . All arguments are the same as documented there.
Name | Description |
entry | Entry
| Entry
[]
A log entry, or array of entries, to write. |
options | WriteOptions
Write options |
Type | Description |
---|---|
void |
const
{
Logging
}
=
require
(
' @google-cloud/logging
'
);
const
logging
=
new
Logging
();
const
log
=
logging
.
logSync
(
'my-log'
);
const
entry
=
log
.
entry
(
'gce_instance'
,
{
instance
:
'my_instance'
});
log
.
critical
(
entry
);
debug(entry, options)
debug
(
entry
:
Entry
|
Entry
[],
options
?:
WriteOptions
)
:
void
;
Write a log entry with a severity of "DEBUG".
This is a simple wrapper around . All arguments are the same as documented there.
Name | Description |
entry | Entry
| Entry
[]
A log entry, or array of entries, to write. |
options | WriteOptions
Write options |
Type | Description |
---|---|
void |
const
{
Logging
}
=
require
(
' @google-cloud/logging
'
);
const
logging
=
new
Logging
();
const
log
=
logging
.
logSync
(
'my-log'
);
const
entry
=
log
.
entry
(
'gce_instance'
,
{
instance
:
'my_instance'
});
log
.
debug
(
entry
);
emergency(entry, options)
emergency
(
entry
:
Entry
|
Entry
[],
options
?:
WriteOptions
)
:
void
;
Write a log entry with a severity of "EMERGENCY".
This is a simple wrapper around . All arguments are the same as documented there.
Name | Description |
entry | Entry
| Entry
[]
A log entry, or array of entries, to write. |
options | WriteOptions
Write options |
Type | Description |
---|---|
void |
const
{
Logging
}
=
require
(
' @google-cloud/logging
'
);
const
logging
=
new
Logging
();
const
log
=
logging
.
logSync
(
'my-log'
);
const
entry
=
log
.
entry
(
'gce_instance'
,
{
instance
:
'my_instance'
});
log
.
emergency
(
entry
);
entry(metadata)
entry
(
metadata
?:
LogEntry
)
:
Entry
;
Create an entry object for this log.
Using this method will not itself do any logging.
Name | Description |
metadata | LogEntry
See a [LogEntry Resource]( https://cloud.google.com/logging/docs/reference/v2/rest/v2/LogEntry ). |
Type | Description |
---|---|
Entry | {Entry} |
const
{
Logging
}
=
require
(
' @google-cloud/logging
'
);
const
logging
=
new
Logging
();
const
log
=
logging
.
logSync
(
'my-log'
);
const
metadata
=
{
resource
:
{
type
:
'gce_instance'
,
labels
:
{
zone
:
'global'
,
instance_id
:
'3'
}
}
};
const
entry
=
log
.
entry
(
metadata
,
{
delegate
:
'my_username'
});
entry(data)
entry
(
data
?:
string
|
{})
:
Entry
;
Name | Description |
data | string | {}
|
Type | Description |
---|---|
Entry |
entry(metadata, data)
entry
(
metadata
?:
LogEntry
,
data
?:
string
|
{})
:
Entry
;
Name | Description |
metadata | LogEntry
|
data | string | {}
|
Type | Description |
---|---|
Entry |
error(entry, options)
error
(
entry
:
Entry
|
Entry
[],
options
?:
WriteOptions
)
:
void
;
Write a log entry with a severity of "ERROR".
This is a simple wrapper around . All arguments are the same as documented there.
Name | Description |
entry | Entry
| Entry
[]
A log entry, or array of entries, to write. |
options | WriteOptions
Write options |
Type | Description |
---|---|
void |
const
{
Logging
}
=
require
(
' @google-cloud/logging
'
);
const
logging
=
new
Logging
();
const
log
=
logging
.
logSync
(
'my-log'
);
const
entry
=
log
.
entry
(
'gce_instance'
,
{
instance
:
'my_instance'
});
log
.
error
(
entry
);
info(entry, options)
info
(
entry
:
Entry
|
Entry
[],
options
?:
WriteOptions
)
:
void
;
Write a log entry with a severity of "INFO".
This is a simple wrapper around . All arguments are the same as documented there.
Name | Description |
entry | Entry
| Entry
[]
A log entry, or array of entries, to write. |
options | WriteOptions
Write options |
Type | Description |
---|---|
void |
const
{
Logging
}
=
require
(
' @google-cloud/logging
'
);
const
logging
=
new
Logging
();
const
log
=
logging
.
logSync
(
'my-log'
);
const
entry
=
log
.
entry
(
'gce_instance'
,
{
instance
:
'my_instance'
});
log
.
info
(
entry
);
notice(entry, options)
notice
(
entry
:
Entry
|
Entry
[],
options
?:
WriteOptions
)
:
void
;
Write a log entry with a severity of "NOTICE".
This is a simple wrapper around . All arguments are the same as documented there.
Name | Description |
entry | Entry
| Entry
[]
A log entry, or array of entries, to write. |
options | WriteOptions
Write options |
Type | Description |
---|---|
void |
const
{
Logging
}
=
require
(
' @google-cloud/logging
'
);
const
logging
=
new
Logging
();
const
log
=
logging
.
logSync
(
'my-log'
);
const
entry
=
log
.
entry
(
'gce_instance'
,
{
instance
:
'my_instance'
});
log
.
notice
(
entry
);
warning(entry, options)
warning
(
entry
:
Entry
|
Entry
[],
options
?:
WriteOptions
)
:
void
;
Write a log entry with a severity of "WARNING".
This is a simple wrapper around . All arguments are the same as documented there.
Name | Description |
entry | Entry
| Entry
[]
A log entry, or array of entries, to write. |
options | WriteOptions
Write options |
Type | Description |
---|---|
void |
const
{
Logging
}
=
require
(
' @google-cloud/logging
'
);
const
logging
=
new
Logging
();
const
log
=
logging
.
logSync
(
'my-log'
);
const
entry
=
log
.
entry
(
'gce_instance'
,
{
instance
:
'my_instance'
});
log
.
warning
(
entry
);
write(entry, opts)
write
(
entry
:
Entry
|
Entry
[],
opts
?:
WriteOptions
)
:
void
;
Write log entries to a custom transport (default: process.stdout).
Name | Description |
entry | Entry
| Entry
[]
A log entry, or array of entries, to write. |
opts | WriteOptions
|
Type | Description |
---|---|
void |
const
entry
=
log
.
entry
(
'gce_instance'
,
{
instance
:
'my_instance'
});
log
.
write
(
entry
);
//-
// You may also pass multiple log entries to write.
//-
const
secondEntry
=
log
.
entry
(
'compute.googleapis.com'
,
{
user
:
'my_username'
});
log
.
write
([
entry
,
secondEntry
]);
//-
// To save some steps, you can also pass in plain values as your entries.
// Note, however, that you must provide a configuration object to specify
// the resource.
//-
const
entries
=
[
{
user
:
'my_username'
},
{
home
:
process
.
env
.
HOME
}
];
const
options
=
{
resource
:
'compute.googleapis.com'
};
log
.
write
(
entries
,
options
);
log
.
write
(
entries
);
});