This module provides support for streaming your Bunyan logs to [Stackdriver Logging]( https://cloud.google.com/logging ).
Inheritance
Writable > LoggingBunyanPackage
@google-cloud/logging-bunyanExamples
Import the client library
const
{
LoggingBunyan
}
=
require
(
' @google-cloud/logging-bunyan
'
);
Create a client that uses Application Default Credentials (ADC) :
const
loggingBunyan
=
new
LoggingBunyan
();
Create a client with explicit credentials :
const
loggingBunyan
=
new
LoggingBunyan
({
projectId
:
'your-project-id'
,
keyFilename
:
'/path/to/keyfile.json'
});
Full quickstart example:
const
bunyan
=
require
(
'bunyan'
);
// Imports the Google Cloud client library for Bunyan
const
{
LoggingBunyan
}
=
require
(
' @google-cloud/logging-bunyan
'
);
// Creates a Bunyan Cloud Logging client
const
loggingBunyan
=
new
LoggingBunyan
();
// Create a Bunyan logger that streams to Cloud Logging
// Logs will be written to: "projects/YOUR_PROJECT_ID/logs/bunyan_log"
const
logger
=
bunyan
.
createLogger
({
// The JSON payload of the log as it appears in Cloud Logging
// will contain "name": "my-service"
name
:
'my-service'
,
streams
:
[
// Log to the console at 'info' and above
{
stream
:
process
.
stdout
,
level
:
'info'
},
// And log to Cloud Logging, logging at 'info' and above
loggingBunyan
.
stream
(
'info'
),
],
});
// Writes some log entries
logger
.
error
(
'warp nacelles offline'
);
logger
.
info
(
'shields at 99%'
);
Constructors
(constructor)(options)
constructor
(
options
?:
types
.
Options
);
Constructs a new instance of the LoggingBunyan
class
options
types. Options
Properties
cloudLog
cloudLog
:
LogSeverityFunctions
;
redirectToStdout
redirectToStdout
:
boolean
;
Methods
_write(record, encoding, callback)
_write
(
record
:
types
.
BunyanLogRecord
,
encoding
:
string
,
callback
:
Function
)
:
void
;
Relay a log entry to the logging agent. This is called by bunyan through Writable#write.
record
types. BunyanLogRecord
encoding
string
callback
Function
void
_writeCall(entries, callback)
_writeCall
(
entries
:
Entry
|
Entry
[],
callback
:
Function
)
:
void
;
A helper function to make a write call
entries
Entry
| Entry
[]
The entries to be written
callback
Function
The callback supplied by Writable.write
void
_writev(chunks, callback)
_writev
(
chunks
:
Array
< {
chunk
:
any
;
encoding
:
string
;
}>,
callback
:
Function
)
:
void
;
Relay an array of log entries to the logging agent. This is called by bunyan through Writable#write.
chunks
Array
<{
chunk: any;
encoding: string;
}>
callback
Function
void
generateCallback(callback)
generateCallback
(
callback
:
Function
)
:
ApiResponseCallback
;
Creates a combined callback which calls the this.defaultCallback and the Writable.write supplied callback
callback
Function
The callback function provided by Writable
ApiResponseCallback
Combined callback which executes both, this.defaultCallback and one supplied by Writable.write
getNodejsLibraryVersion()
getNodejsLibraryVersion
()
:
string
;
Method used to retrieve the current logging-bunyan library version stored in NODEJS_BUNYAN_DEFAULT_LIBRARY_VERSION
string
The version of this library
properLabels(labels)
static
properLabels
(
labels
:
any
)
:
boolean
;
labels
any
boolean
stream(level)
stream
(
level
:
types
.
LogLevel
)
:
types
.
StreamResponse
;
Convenience method that Builds a bunyan stream object that you can put in the bunyan streams list.
level
types. LogLevel
types. StreamResponse
write(record, callback)
write
(
record
:
types
.
BunyanLogRecord
,
callback
?:
Function
)
:
boolean
;
Intercept log entries as they are written so we can attempt to add the trace ID in the same continuation as the function that wrote the log, because the trace agent currently uses continuation local storage for the trace context.
By the time the Writable stream buffer gets flushed and _write gets called we may well be in a different continuation.
record
types. BunyanLogRecord
callback
Function
boolean
write(record, encoding, callback)
write
(
record
:
types
.
BunyanLogRecord
,
encoding
?:
string
,
callback
?:
Function
)
:
boolean
;
record
types. BunyanLogRecord
encoding
string
callback
Function
boolean