Skip to content

Commit

Permalink
Merge pull request #844 from traPtitech/logging
Browse files Browse the repository at this point in the history
  • Loading branch information
H1rono authored Jan 3, 2025
2 parents 2428d70 + e0be10b commit 702f49e
Show file tree
Hide file tree
Showing 27 changed files with 540 additions and 371 deletions.
32 changes: 32 additions & 0 deletions logging/context.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package logging

import (
"context"
"errors"

"go.uber.org/zap"
)

type contextKey struct{}

var ErrLoggerNotSet = errors.New("logger not set in context")

func SetLogger(ctx context.Context, logger *zap.Logger) context.Context {
return context.WithValue(ctx, contextKey{}, logger)
}

func GetLoggerMaybe(ctx context.Context) (*zap.Logger, error) {
logger, ok := ctx.Value(contextKey{}).(*zap.Logger)
if !ok {
return nil, ErrLoggerNotSet
}
return logger, nil
}

func GetLogger(ctx context.Context) *zap.Logger {
logger, err := GetLoggerMaybe(ctx)
if err != nil {
panic(err)
}
return logger
}
36 changes: 36 additions & 0 deletions logging/load.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package logging

import (
"errors"
"os"

"go.uber.org/zap"
)

type Mode string

const (
Development Mode = "development"
Production Mode = "production"
)

var ErrUnknownMode = errors.New("unknown mode")

func Load(mode Mode) (*zap.Logger, error) {
switch mode {
case Development:
return zap.NewDevelopment()
case Production:
return zap.NewProduction()
default:
return nil, ErrUnknownMode
}
}

func ModeFromEnv(varName string) Mode {
// TODO: strconv.ParseBool を使う
if os.Getenv(varName) != "" {
return Development
}
return Production
}
16 changes: 6 additions & 10 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"os"

"github.com/traPtitech/Jomon/logging"
"github.com/traPtitech/Jomon/model"
"github.com/traPtitech/Jomon/router"
"github.com/traPtitech/Jomon/storage"
Expand Down Expand Up @@ -41,28 +42,23 @@ func main() {
repo := model.NewEntRepository(client, strg)

// Setup server
var logger *zap.Logger
if os.Getenv("IS_DEBUG_MODE") != "" {
logger, err = zap.NewDevelopment()
} else {
logger, err = zap.NewProduction()
logMode := logging.ModeFromEnv("IS_DEBUG_MODE")
logger, err := logging.Load(logMode)
if err != nil {
panic(err)
}
defer func() {
if err := logger.Sync(); err != nil {
panic(err)
}
}()
if err != nil {
panic(err)
}
handlers := router.Handlers{
Repository: repo,
Storage: strg,
Logger: logger,
SessionName: "session",
}

server := router.NewServer(handlers)
server := handlers.NewServer(logger)

port := os.Getenv("PORT")
if port == "" {
Expand Down
8 changes: 4 additions & 4 deletions model/admin_impl_test.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
package model

import (
"context"
"testing"

"github.com/google/uuid"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/traPtitech/Jomon/testutil"
"github.com/traPtitech/Jomon/testutil/random"
)

func TestEntRepository_GetAdmins(t *testing.T) {
ctx := context.Background()
ctx := testutil.NewContext(t)
client, storage, err := setup(t, ctx, "get_admins")
require.NoError(t, err)
repo := NewEntRepository(client, storage)
Expand Down Expand Up @@ -53,7 +53,7 @@ func TestEntRepository_GetAdmins(t *testing.T) {
}

func TestEntRepository_AddAdmins(t *testing.T) {
ctx := context.Background()
ctx := testutil.NewContext(t)
client, storage, err := setup(t, ctx, "add_admins")
require.NoError(t, err)
repo := NewEntRepository(client, storage)
Expand All @@ -77,7 +77,7 @@ func TestEntRepository_AddAdmins(t *testing.T) {
}

func TestEntRepository_DeleteAdmins(t *testing.T) {
ctx := context.Background()
ctx := testutil.NewContext(t)
client, storage, err := setup(t, ctx, "delete_admins")
require.NoError(t, err)
repo := NewEntRepository(client, storage)
Expand Down
9 changes: 4 additions & 5 deletions model/comment_impl_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package model

import (
"context"
"testing"
"time"

Expand All @@ -14,7 +13,7 @@ import (
)

func TestEntRepository_GetComments(t *testing.T) {
ctx := context.Background()
ctx := testutil.NewContext(t)
client, storage, err := setup(t, ctx, "get_comments")
require.NoError(t, err)
repo := NewEntRepository(client, storage)
Expand Down Expand Up @@ -83,7 +82,7 @@ func TestEntRepository_GetComments(t *testing.T) {
}

func TestEntRepository_CreateComment(t *testing.T) {
ctx := context.Background()
ctx := testutil.NewContext(t)
client, storage, err := setup(t, ctx, "create_comment")
require.NoError(t, err)
repo := NewEntRepository(client, storage)
Expand Down Expand Up @@ -159,7 +158,7 @@ func TestEntRepository_CreateComment(t *testing.T) {
}

func TestEntRepository_UpdateComment(t *testing.T) {
ctx := context.Background()
ctx := testutil.NewContext(t)
client, storage, err := setup(t, ctx, "update_comment")
require.NoError(t, err)
repo := NewEntRepository(client, storage)
Expand Down Expand Up @@ -285,7 +284,7 @@ func TestEntRepository_UpdateComment(t *testing.T) {
}

func TestEntRepository_DeleteComment(t *testing.T) {
ctx := context.Background()
ctx := testutil.NewContext(t)
client, storage, err := setup(t, ctx, "delete_comment")
require.NoError(t, err)
repo := NewEntRepository(client, storage)
Expand Down
21 changes: 10 additions & 11 deletions model/file_impl_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package model

import (
"context"
"testing"
"time"

Expand All @@ -14,14 +13,14 @@ import (
)

func TestEntRepository_CreateFile(t *testing.T) {
ctx := context.Background()
ctx := testutil.NewContext(t)
client, storage, err := setup(t, ctx, "create_file")
require.NoError(t, err)
repo := NewEntRepository(client, storage)

t.Run("Success", func(t *testing.T) {
t.Parallel()
ctx := context.Background()
ctx := testutil.NewContext(t)

var tags []*Tag
var targets []*RequestTarget
Expand Down Expand Up @@ -60,7 +59,7 @@ func TestEntRepository_CreateFile(t *testing.T) {

t.Run("UnknownRequest", func(t *testing.T) {
t.Parallel()
ctx := context.Background()
ctx := testutil.NewContext(t)

user, err := repo.CreateUser(
ctx,
Expand All @@ -82,7 +81,7 @@ func TestEntRepository_CreateFile(t *testing.T) {

t.Run("MissingName", func(t *testing.T) {
t.Parallel()
ctx := context.Background()
ctx := testutil.NewContext(t)

var tags []*Tag
var targets []*RequestTarget
Expand All @@ -109,14 +108,14 @@ func TestEntRepository_CreateFile(t *testing.T) {
}

func TestEntRepository_GetFile(t *testing.T) {
ctx := context.Background()
ctx := testutil.NewContext(t)
client, storage, err := setup(t, ctx, "get_file")
require.NoError(t, err)
repo := NewEntRepository(client, storage)

t.Run("Success", func(t *testing.T) {
t.Parallel()
ctx := context.Background()
ctx := testutil.NewContext(t)

var tags []*Tag
var targets []*RequestTarget
Expand Down Expand Up @@ -156,22 +155,22 @@ func TestEntRepository_GetFile(t *testing.T) {

t.Run("UnknownFile", func(t *testing.T) {
t.Parallel()
ctx := context.Background()
ctx := testutil.NewContext(t)

_, err = repo.GetFile(ctx, uuid.New())
assert.Error(t, err)
})
}

func TestEntRepository_DeleteFile(t *testing.T) {
ctx := context.Background()
ctx := testutil.NewContext(t)
client, storage, err := setup(t, ctx, "delete_file")
require.NoError(t, err)
repo := NewEntRepository(client, storage)

t.Run("Success", func(t *testing.T) {
t.Parallel()
ctx := context.Background()
ctx := testutil.NewContext(t)

var tags []*Tag
var targets []*RequestTarget
Expand Down Expand Up @@ -207,7 +206,7 @@ func TestEntRepository_DeleteFile(t *testing.T) {

t.Run("UnknownFile", func(t *testing.T) {
t.Parallel()
ctx := context.Background()
ctx := testutil.NewContext(t)

err = repo.DeleteFile(ctx, uuid.New())
assert.Error(t, err)
Expand Down
Loading

0 comments on commit 702f49e

Please sign in to comment.