Skip to content

Commit

Permalink
logger Default alias
Browse files Browse the repository at this point in the history
  • Loading branch information
quentinguidee committed Sep 10, 2023
1 parent 6467d16 commit 35606b3
Show file tree
Hide file tree
Showing 21 changed files with 104 additions and 84 deletions.
14 changes: 7 additions & 7 deletions adapter/instance_fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func NewInstanceFSAdapter() types.InstanceAdapterPort {

err := os.MkdirAll(adapter.instancesPath, os.ModePerm)
if err != nil && !os.IsExist(err) {
log.Default.Error(err,
log.Error(err,
vlog.String("message", "failed to create directory"),
vlog.String("path", adapter.instancesPath),
)
Expand Down Expand Up @@ -108,7 +108,7 @@ func (a *InstanceFSAdapter) LoadSettings(i *types.Instance) error {
settingsBytes, err := os.ReadFile(settingsPath)

if errors.Is(err, os.ErrNotExist) {
log.Default.Warn("instance_settings.json not found. using default.")
log.Warn("instance_settings.json not found. using default.")
} else if err != nil {
return err
} else {
Expand All @@ -124,7 +124,7 @@ func (a *InstanceFSAdapter) LoadSettings(i *types.Instance) error {
func (a *InstanceFSAdapter) ReadService(instancePath string) (types.Service, error) {
data, err := os.ReadFile(path.Join(instancePath, ".vertex", "service.yml"))
if err != nil {
log.Default.Warn("'.vertex/service.yml' file not found",
log.Warn("'.vertex/service.yml' file not found",
vlog.String("path", path.Dir(instancePath)),
)
}
Expand Down Expand Up @@ -182,27 +182,27 @@ func (a *InstanceFSAdapter) LoadEnv(i *types.Instance) error {
func (a *InstanceFSAdapter) Reload(load func(uuid uuid.UUID)) {
entries, err := os.ReadDir(a.instancesPath)
if err != nil {
log.Default.Error(err)
log.Error(err)
os.Exit(1)
}

for _, entry := range entries {
info, err := entry.Info()
if err != nil {
log.Default.Error(err)
log.Error(err)
continue
}

isInstance := entry.IsDir() || info.Mode()&os.ModeSymlink != 0

if isInstance {
log.Default.Info("found instance",
log.Info("found instance",
vlog.String("uuid", entry.Name()),
)

id, err := uuid.Parse(entry.Name())
if err != nil {
log.Default.Error(err)
log.Error(err)
continue
}

Expand Down
10 changes: 5 additions & 5 deletions adapter/instance_logs_fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func (a *InstanceLogsFSAdapter) Close(uuid uuid.UUID) error {
func (a *InstanceLogsFSAdapter) Push(uuid uuid.UUID, line types.LogLine) {
l, err := a.getLogger(uuid)
if err != nil {
log.Default.Error(err)
log.Error(err)
return
}
l.currentLine += 1
Expand All @@ -86,7 +86,7 @@ func (a *InstanceLogsFSAdapter) Push(uuid uuid.UUID, line types.LogLine) {

_, err = fmt.Fprintf(l.file, "%s\n", line.Message)
if err != nil {
log.Default.Error(err)
log.Error(err)
}
}

Expand Down Expand Up @@ -127,17 +127,17 @@ func (a *InstanceLogsFSAdapter) startCron() {
for id := range a.loggers {
err := a.Close(id)
if err != nil {
log.Default.Error(err)
log.Error(err)
continue
}
err = a.Open(id)
if err != nil {
log.Default.Error(err)
log.Error(err)
}
}
})
if err != nil {
log.Default.Error(err)
log.Error(err)
return
}
s.StartAsync()
Expand Down
2 changes: 1 addition & 1 deletion adapter/package_fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func NewPackageFSAdapter(params *PackageFSAdapterParams) types.PackageAdapterPor

err := adapter.Reload()
if err != nil {
log.Default.Error(fmt.Errorf("failed to reload services: %v", err))
log.Error(fmt.Errorf("failed to reload services: %v", err))
}
return adapter
}
Expand Down
8 changes: 4 additions & 4 deletions adapter/proxy_fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func NewProxyFSAdapter(params *ProxyFSAdapterParams) types.ProxyAdapterPort {

err := os.MkdirAll(params.proxyPath, os.ModePerm)
if err != nil && !os.IsExist(err) {
log.Default.Error(err,
log.Error(err,
vlog.String("message", "failed to create directory"),
vlog.String("path", params.proxyPath),
)
Expand Down Expand Up @@ -67,15 +67,15 @@ func (a *ProxyFSRepository) read() {
file, err := os.ReadFile(p)

if errors.Is(err, os.ErrNotExist) {
log.Default.Info("redirects.json doesn't exists or could not be found")
log.Info("redirects.json doesn't exists or could not be found")
} else if err != nil {
log.Default.Error(err)
log.Error(err)
return
}

err = json.Unmarshal(file, &a.redirects)
if err != nil {
log.Default.Error(err)
log.Error(err)
return
}
}
Expand Down
22 changes: 11 additions & 11 deletions adapter/runner_docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ type dockerMessage struct {
func NewRunnerDockerAdapter() RunnerDockerAdapter {
cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
if err != nil {
log.Default.Warn("couldn't connect with the Docker cli.",
log.Warn("couldn't connect with the Docker cli.",
vlog.String("error", err.Error()),
)

Expand Down Expand Up @@ -80,7 +80,7 @@ func (a RunnerDockerAdapter) Start(instance *types.Instance, onLog func(msg stri
if errors.Is(err, ErrContainerNotFound) {
containerName := instance.DockerContainerName()

log.Default.Info("container doesn't exists, create it.",
log.Info("container doesn't exists, create it.",
vlog.String("container_name", containerName),
)

Expand Down Expand Up @@ -228,12 +228,12 @@ func (a RunnerDockerAdapter) CheckForUpdates(instance *types.Instance) error {
}

if latestImageID == currentImageID {
log.Default.Info("already up-to-date",
log.Info("already up-to-date",
vlog.String("uuid", instance.UUID.String()),
)
instance.Update = nil
} else {
log.Default.Info("a new update is available",
log.Info("a new update is available",
vlog.String("uuid", instance.UUID.String()),
)
instance.Update = &types.InstanceUpdate{
Expand Down Expand Up @@ -344,7 +344,7 @@ func (a RunnerDockerAdapter) buildImageFromDockerfile(instancePath string, image
msg := dockerMessage{}
err := json.Unmarshal(scanner.Bytes(), &msg)
if err != nil {
log.Default.Warn("Failed to parse message",
log.Warn("Failed to parse message",
vlog.String("message", scanner.Text()),
)
} else {
Expand All @@ -354,7 +354,7 @@ func (a RunnerDockerAdapter) buildImageFromDockerfile(instancePath string, image
}
}

log.Default.Info("Docker build: success.")
log.Info("Docker build: success.")
return nil
}

Expand Down Expand Up @@ -388,7 +388,7 @@ func (a RunnerDockerAdapter) createContainer(options createContainerOptions) (st

res, err := a.cli.ContainerCreate(context.Background(), &config, &hostConfig, nil, nil, options.containerName)
for _, warn := range res.Warnings {
log.Default.Warn("warning while creating container",
log.Warn("warning while creating container",
vlog.String("warning", warn),
)
}
Expand All @@ -402,12 +402,12 @@ func (a RunnerDockerAdapter) watchForStatusChange(containerID string, instance *
select {
case err := <-errChan:
if err != nil {
log.Default.Error(err,
log.Error(err,
vlog.String("uuid", instance.UUID.String()),
)
}
case status := <-resChan:
log.Default.Info("container exited",
log.Info("container exited",
vlog.String("uuid", instance.UUID.String()),
vlog.Int64("status", status.StatusCode),
)
Expand All @@ -426,7 +426,7 @@ func (a RunnerDockerAdapter) watchForLogs(containerID string, instance *types.In
Tail: "0",
})
if err != nil {
log.Default.Error(err,
log.Error(err,
vlog.String("uuid", instance.UUID.String()),
)
}
Expand All @@ -436,7 +436,7 @@ func (a RunnerDockerAdapter) watchForLogs(containerID string, instance *types.In
onLog(scanner.Text())
}
_ = logs.Close()
log.Default.Info("logs pipe closed",
log.Info("logs pipe closed",
vlog.String("uuid", instance.UUID.String()),
)
}()
Expand Down
4 changes: 2 additions & 2 deletions adapter/runner_fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func (a RunnerFSAdapter) Delete(instance *types.Instance) error {

func (a RunnerFSAdapter) Start(instance *types.Instance, onLog func(msg string), onErr func(msg string), setStatus func(status string)) error {
if a.commands[instance.UUID] != nil {
log.Default.Error(errors.New("runner already started"),
log.Error(errors.New("runner already started"),
vlog.String("name", instance.Name),
)
}
Expand Down Expand Up @@ -102,7 +102,7 @@ func (a RunnerFSAdapter) Start(instance *types.Instance, onLog func(msg string),
go func() {
err := cmd.Wait()
if err != nil {
log.Default.Error(err,
log.Error(err,
vlog.String("name", instance.Service.Name),
)
}
Expand Down
2 changes: 1 addition & 1 deletion adapter/service_fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func NewServiceFSAdapter(params *ServiceFSAdapterParams) types.ServiceAdapterPor
}
err := adapter.Reload()
if err != nil {
log.Default.Error(fmt.Errorf("failed to reload services: %v", err))
log.Error(fmt.Errorf("failed to reload services: %v", err))
}
return adapter
}
Expand Down
6 changes: 3 additions & 3 deletions adapter/settings_fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func NewSettingsFSAdapter(params *SettingsFSAdapterParams) types.SettingsAdapter

err := os.MkdirAll(params.settingsPath, os.ModePerm)
if err != nil && !os.IsExist(err) {
log.Default.Error(err,
log.Error(err,
vlog.String("message", "failed to create directory"),
vlog.String("path", params.settingsPath),
)
Expand All @@ -44,7 +44,7 @@ func NewSettingsFSAdapter(params *SettingsFSAdapterParams) types.SettingsAdapter

err = adapter.read()
if err != nil {
log.Default.Error(err)
log.Error(err)
}

return adapter
Expand Down Expand Up @@ -74,7 +74,7 @@ func (a *SettingsFSAdapter) read() error {
file, err := os.ReadFile(p)

if errors.Is(err, os.ErrNotExist) {
log.Default.Info("settings.json doesn't exists or could not be found")
log.Info("settings.json doesn't exists or could not be found")
return nil
} else if err != nil {
return err
Expand Down
10 changes: 5 additions & 5 deletions init.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,19 @@ var (
func main() {
defer log.Default.Close()

log.Default.Info("Vertex starting...")
log.Info("Vertex starting...")

parseArgs()

err := setupDependencies()
if err != nil {
log.Default.Error(fmt.Errorf("failed to setup dependencies: %v", err))
log.Error(fmt.Errorf("failed to setup dependencies: %v", err))
return
}

err = config.Current.Apply()
if err != nil {
log.Default.Error(fmt.Errorf("failed to apply the current configuration: %v", err))
log.Error(fmt.Errorf("failed to apply the current configuration: %v", err))
return
}

Expand All @@ -54,7 +54,7 @@ func main() {
// Logs
url := fmt.Sprintf("http://%s", config.Current.Host)
fmt.Printf("\n-- Vertex Client :: %s\n\n", url)
log.Default.Info("Vertex started",
log.Info("Vertex started",
vlog.String("url", url),
)

Expand Down Expand Up @@ -98,7 +98,7 @@ func setupDependencies() error {
for _, dep := range dependencies {
err := setupDependency(dep)
if err != nil {
log.Default.Error(err)
log.Error(err)
os.Exit(1)
}
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/ginutils/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
func Logger(router string) gin.HandlerFunc {
return gin.LoggerWithFormatter(func(params gin.LogFormatterParams) string {
if params.ErrorMessage != "" {
log.Default.Request("request",
log.Request("request",
vlog.String("router", router),
vlog.String("method", params.Method),
vlog.Int("status", params.StatusCode),
Expand All @@ -20,7 +20,7 @@ func Logger(router string) gin.HandlerFunc {
vlog.String("error", params.ErrorMessage),
)
} else {
log.Default.Request("request",
log.Request("request",
vlog.String("router", router),
vlog.String("method", params.Method),
vlog.Int("status", params.StatusCode),
Expand Down
20 changes: 20 additions & 0 deletions pkg/log/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,23 @@ var Default = *vlog.New(
vlog.WithOutputFile("live/logs", vlog.LogFormatText),
vlog.WithOutputFile("live/logs", vlog.LogFormatJson),
)

func Debug(msg string, fields ...vlog.KeyValue) {
Default.Debug(msg, fields...)
}

func Info(msg string, fields ...vlog.KeyValue) {
Default.Info(msg, fields...)
}

func Warn(msg string, fields ...vlog.KeyValue) {
Default.Warn(msg, fields...)
}

func Error(err error, fields ...vlog.KeyValue) {
Default.Error(err, fields...)
}

func Request(msg string, fields ...vlog.KeyValue) {
Default.Request(msg, fields...)
}
6 changes: 3 additions & 3 deletions pkg/storage/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ var (
)

func CloneOrPullRepository(url string, dest string) error {
log.Default.Info("downloading repository",
log.Info("downloading repository",
vlog.String("url", url),
)

Expand Down Expand Up @@ -57,7 +57,7 @@ func CloneOrPullRepository(url string, dest string) error {
}

func DownloadLatestGithubRelease(owner string, repo string, dest string) error {
log.Default.Info("downloading repository",
log.Info("downloading repository",
vlog.String("owner", owner),
vlog.String("repo", repo),
)
Expand All @@ -73,7 +73,7 @@ func DownloadLatestGithubRelease(owner string, repo string, dest string) error {
}

func DownloadGithubRelease(release *github.RepositoryRelease, dest string) error {
log.Default.Info("downloading release",
log.Info("downloading release",
vlog.String("release", *release.Name),
)

Expand Down
Loading

0 comments on commit 35606b3

Please sign in to comment.