Comments (3)
I'm finding the same behavior with https://github.com/uber-go/zap. Logs are non-deterministically picked up from stderr
by the CloudWatch Logs agent.
zap
opens stderr
as a write-only file, in append, asynchronous I/O mode. I think Lambda has trouble picking up the async I/O. zerolog
may be doing something similar.
In your example, you could add a os.Stderr.Sync()
before exiting or after writing a line.
var handlerInitialized = false
var chiAdapter *chiadapter.ChiLambda
var logger *zerolog.Logger
func Handler(req events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) {
if !handlerInitialized {
logger = zerolog.New(os.Stderr)
logger.Info("cold_start")
api := server.NewAPI(&logger)
chiAdapter = chiadapter.New(api.Router())
handlerInitialized = true
}
return chiAdapter.Proxy(req)
}
func main() {
lambda.Start(Handler)
if err := os.Stderr.Sync(); err != nil {
log.Println(err)
} // force synchronous write to /dev/stderr before exiting
}
from aws-lambda-go.
@tonyghita I will try to add os.Stderr.Sync()
but i found that sometimes any code running after lambda.Start(Handler)
will not work.
from aws-lambda-go.
Ahh, good point. lambda.Start()
won't return.
I created a hook using zap
to sync os.Stderr
after every log entry.
// flush() is a logger Hook for flushing the log file after every entry.
// This is more expensive than using the default asynchronous I/O, but
// necessary for Lambda to pick up each log line.
// Don't do this in performance-sensitive applications.
func flush(f *os.File) func(zapcore.Entry) error {
return func(zapcore.Entry) error {
if f != nil {
_ = f.Sync()
}
return nil
}
}
// New creates a new logger that writes to /dev/stderr and syncs after every log entry.
// The syncing is necessary because AWS Lambda does not handle asynchronous I/O well.
func New() (*Logger, error) {
log, err := zap.NewProduction(zap.Hooks(flush(os.Stderr)))
if err != nil {
return nil, err
}
return &Logger{log: log}, nil
}
I'm not familiar with zerolog, but maybe it provides a similar functionality?
from aws-lambda-go.
Related Issues (20)
- events.CodeCommitReference has missing Deleted field HOT 2
- ALB target grouped Lambda returns fine but alb return 502 error that "LambdaInvalidResponse" HOT 2
- Deserialisation of array values in Cognito events custom UserAttributes not working correctly HOT 1
- Support structured JSON logging for errors HOT 2
- please add a mocked example HOT 1
- Custom runtime provided.al2 broken on v1.42 HOT 1
- Add IoTCoreMQTTMessage to aws/aws-lambda-go/events
- Add disconnectStatusCode to event/APIGatewayWebsocketProxyRequestContext
- RequestContext.Authorizer differs between REST gateway vs HTTP gateway
- UnmarshalTypeError on CodePipelineEventBridgeEvent HOT 4
- Missing field enableSMSMFA on struct events.CognitoEventUserPoolsMigrateUserResponse
- Runtime.InvalidEntrypoint
- Add AWS CloudWatch Alarm Event
- Implement `MultiValueHeaders` for ``events.LambdaFunctionURLResponse``
- Small improvement in APIGatewayWebsocketProxyRequestContext.Authorizer
- [BUG] Traceability panic error memory address runtime al2023
- Getting Segmentation Fault while using Lambda function with SAM Server
- Lambda Function Not Routing Correctly with API Gateway
- Please export extensionAPIClient HOT 1
- events.EventBridgeEvent is returning an empty payload when called by the EventBridge Scheduler HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from aws-lambda-go.