Created
June 14, 2017 06:09
-
-
Save jaredjenkins/b49f064693050f93bf4a442aad611d76 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
//#cgo pkg-config: python2.7 | |
//#include <Python.h> | |
//static PyObject *runtime_log_fn; | |
// | |
//void runtime_log(char *msg) | |
//{ | |
// if (runtime_log_fn == NULL) { | |
// free(msg); | |
// return; | |
// } | |
// PyObject *tmp = PyObject_CallFunction(runtime_log_fn, "s", msg); | |
// if (tmp != NULL) { | |
// Py_DECREF(tmp); | |
// } | |
// free(msg); | |
//} | |
import "C" | |
import ( | |
"go.uber.org/zap" | |
"github.com/eawsy/aws-lambda-go-core/service/lambda/runtime" | |
"time" | |
"go.uber.org/zap/zapcore" | |
"log" | |
"sync" | |
) | |
func init() { | |
log.SetFlags(0) | |
} | |
var lock sync.Mutex | |
type CMarshalledWriter struct { } | |
func (*CMarshalledWriter) Write(p []byte) (int, error) { | |
lock.Lock() | |
defer lock.Unlock() | |
C.runtime_log(C.CString(string(p))) | |
return len(p), nil | |
} | |
func (*CMarshalledWriter) Sync() error { return nil } | |
func logger() *zap.Logger { | |
encoderConfig := zap.NewProductionEncoderConfig() | |
encoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder | |
encoder := zapcore.NewJSONEncoder(encoderConfig) | |
zapCore := zapcore.NewCore(encoder, new(CMarshalledWriter), zap.NewAtomicLevelAt(zap.InfoLevel)) | |
logger := zap.New(zapCore) | |
return logger | |
} | |
func Handle(evt interface{}, ctx *runtime.Context) (string, error) { | |
logger := logger() | |
defer logger.Sync() | |
logger.Info("A sample log", | |
// Structured context as strongly-typed Field values. | |
zap.Time("time", time.Now()), | |
zap.String("requestId", ctx.AWSRequestID), | |
) | |
return "Test 2", nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment