{"id":15797097,"url":"https://github.com/mylxsw/asteria","last_synced_at":"2025-08-16T04:31:36.736Z","repository":{"id":57485416,"uuid":"197136729","full_name":"mylxsw/asteria","owner":"mylxsw","description":"Asteria is a logging library for go.","archived":false,"fork":false,"pushed_at":"2022-12-22T06:58:03.000Z","size":119,"stargazers_count":4,"open_issues_count":0,"forks_count":4,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-10-12T00:22:46.345Z","etag":null,"topics":["go","golang","log","logging"],"latest_commit_sha":null,"homepage":"https://godoc.org/github.com/mylxsw/asteria","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/mylxsw.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-07-16T06:47:18.000Z","updated_at":"2023-03-04T14:11:44.000Z","dependencies_parsed_at":"2023-01-30T06:00:36.595Z","dependency_job_id":null,"html_url":"https://github.com/mylxsw/asteria","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mylxsw%2Fasteria","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mylxsw%2Fasteria/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mylxsw%2Fasteria/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mylxsw%2Fasteria/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mylxsw","download_url":"https://codeload.github.com/mylxsw/asteria/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":229710739,"owners_count":18111641,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["go","golang","log","logging"],"created_at":"2024-10-05T00:03:51.760Z","updated_at":"2024-12-16T18:06:39.357Z","avatar_url":"https://github.com/mylxsw.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Asteria \n\n[![Build Status](https://www.travis-ci.org/mylxsw/asteria.svg?branch=master)](https://www.travis-ci.org/mylxsw/asteria)\n[![Coverage Status](https://coveralls.io/repos/github/mylxsw/asteria/badge.svg?branch=master)](https://coveralls.io/github/mylxsw/asteria?branch=master)\n[![Go Report Card](https://goreportcard.com/badge/github.com/mylxsw/asteria)](https://goreportcard.com/report/github.com/mylxsw/asteria)\n[![codecov](https://codecov.io/gh/mylxsw/asteria/branch/master/graph/badge.svg)](https://codecov.io/gh/mylxsw/asteria)\n[![GoDoc](https://godoc.org/github.com/mylxsw/asteria?status.svg)](https://godoc.org/github.com/mylxsw/asteria)\n[![Sourcegraph](https://sourcegraph.com/github.com/mylxsw/asteria/-/badge.svg)](https://sourcegraph.com/github.com/mylxsw/asteria?badge)\n[![GitHub](https://img.shields.io/github/license/mylxsw/asteria.svg)](https://github.com/mylxsw/asteria)\n\n**Asteria** is a logging library for go.\n\nThe most straightforward way to write a log\n\n    log.Debug(\"Drizzle breeze shore, dangerous night boat\")\n    log.Error(\"On the moon, the willow head, after the evening\")\n    log.WithFields(log.Fields{\n        \"user_id\":  123,\n        \"username\": \"Tom\",\n    }).Warningf(\"The gentleman is frank, the villain is often jealous.\")\n    log.F(log.M{\n        \"user_id\":  123,\n        \"username\": \"Tom\",\n    }).Warningf(\"The gentleman is frank, the villain is often jealous.\")\n\nLog according to different modules\n\n    var logger = log.Module(\"asteria.user.enterprise.jobs\")\n       \n    logger.Debug(\"Drizzle breeze shore, dangerous night boat\")\n    logger.Error(\"On the moon, the willow head, after the evening\")\n    logger.WithFields(log.Fields{\n        \"user_id\":  123,\n        \"username\": \"Tom\",\n    }).Warningf(\"The gentleman is frank, the villain is often jealous.\")\n    logger.F(log.M{\n        \"user_id\":  123,\n        \"username\": \"Tom\",\n    }).Warningf(\"The gentleman is frank, the villain is often jealous.\")\n    \n## Install\n\n    go get -u github.com/mylxsw/asteria/log\n\n## Customize\n\n## Write the line number of the file for caller\n\n    // default display file line number \n    log.DefaultWithFileLine(true)\n    // display file line number for individual modules\n    log.Module(\"asteria\").WithFileLine(true)\n\n### Filter\n\nThe filter supports separate settings for the specified module or global settings. With Filter, you can modify the log or cancel the log output before the log formatted output.\n\nWhen multiple Filters are specified, multiple Filters are executed in the order they were added, and Global Filter takes precedence over a separate Filter set for the module.\n\n#### Global Filter\n\n    log.AddGlobalFilter(func(filter log.Filter) log.Filter {\n        return func(f formatter.Format) {\n            // if f.Level == level.Debug {\n            //     return\n            // }\n            \n            f.Fields.CustomFields[\"user_id\"] = 123\n            // Not calling filter(f) will cancel the output of the log\n            filter(f)\n        }\n    })\n\n#### Module Filter\n\n    var logger = log.Module(\"asteria\")\n    logger.AddFilter(func(filter log.Filter) log.Filter {\n        return func(f formatter.Format) {\n            // filter(f)\n            f.Level = level.Emergency\n            filter(f)\n        }\n    })\n\n### Log Formatter\n\nAsteria supports custom log formats, just implement the `formatter.Formatter` interface.\n    \n    type Formatter interface {\n        Format(f Format) string\n    }\n\nThree types of log formatting methods are provided by default\n\n- text format, the default mode\n- JSON with time\n- JSON\n\n#### Text\n\nUse the default format, no need to make any settings, you can also specify\n\n    // Set the default module log format\n    log.Formatter(formatter.NewDefaultFormatter(true))\n    // Or\n    log.Default().Formatter(formatter.NewDefaultFormatter())\n    // Set the log format of the specified module\n    log.Module(\"asteria\").Formatter(formatter.NewDefaultFormatter())\n\nThe format is as follows\n\n    [RFC3339 formatted time] [logLevel] moduleName logMessage {logContext}\n\nThe module name field is specified using the `log.Module` method and the default log module is automatically generated based on the package name of the caller. Context information mainly consists of two parts\n\n- Fields starting with `#` are automatically set by the system\n- Other fields are context information set by the user using `WithFields`\n\nSample log output\n\n![](https://ssl.aicode.cc/2019-07-17-15633539363228.jpg)\n\n\u003e You can set the default color output by `log.DefaultLogFormatter(formatter.NewDefaultFormatter(false))` or set a module to turn off color output via `log.Module(\"asteria\").Formatter(formatter.NewDefaultFormatter(false))`.\n\n#### Json with Time\n\n    // Set the default module log format\n    log.Formatter(formatter.NewJSONWithTimeFormatter())\n    // Or\n    log.Default().Formatter(formatter.NewJSONWithTimeFormatter())\n    // Set the log format of the specified module\n    log.Module(\"asteria\").Formatter(formatter.NewJSONWithTimeFormatter())\n \nSample log output\n\n    [2019-07-17T16:58:24+08:00] {\"module\":\"user\",\"level_name\":\"ERROR\",\"level\":400,\"context\":{\"#ref\":\"190101931\",\"user_id\":123},\"message\":\"user create failed\",\"datetime\":\"2019-07-17T16:58:24+08:00\"}\n    \n#### Json \n\n    // Set the default module log format\n    log.Formatter(formatter.NewJSONFormatter())\n    // Or\n    log.Default().Formatter(formatter.NewJSONFormatter())\n    // Set the log format of the specified module\n    log.Module(\"asteria\").Formatter(formatter.NewJSONFormatter())\n\nSample log output\n\n    {\"module\":\"asteria.user.enterprise.jobs\",\"level_name\":\"EMERGENCY\",\"level\":600,\"context\":{\"#file\":\"/Users/mylxsw/codes/github/asteria/log_test.go\",\"#func\":\"TestModule\",\"#line\":91,\"#package\":\"github.com/mylxsw/asteria_test\",\"#ref\":\"190101931\",\"user_id\":123},\"message\":\"He remembered the count of Monte cristo\",\"datetime\":\"2019-07-17T16:58:24+08:00\"}\n\n\n### Log Writer\n\nAsteria supports custom log output mode, only need to implement `writer.Writer` interface.\n    \n    type Writer interface {\n        Write(le level.Level, module string, message string) error\n        ReOpen() error\n        Close() error\n    }\n\n\n#### Stdout\n\nThe default output mode is **standard output**, no need to make any settings, of course, you can also specify\n\n    // Set the default module log output\n    log.Writer(writer.NewStdoutWriter())\n    // can also be like this\n    log.Default().Writer(writer.NewStdoutWriter())\n    // Set the log format of the specified module\n    log.Module(\"asteria\").Writer(writer.NewStdoutWriter())\n\n#### File\n\n    // Set the default module log output\n    log.Writer(writer.NewSingleFileWriter(\"/var/log/asteria.log\"))\n    // can also be like this\n    log.Default().Writer(writer.NewSingleFileWriter(\"/var/log/asteria.log\"))\n    // Set the log format of the specified module\n    log.Module(\"asteria\").Writer(writer.NewSingleFileWriter(\"/var/log/asteria.log\"))\n    \n\nIf you want to rotate the logs according to your own rules, such as generating new log files every day, you can use `RotatingFileWriter`\n\n    fw := writer.NewDefaultRotatingFileWriter(context.TODO(), func(le level.Level, module string) string {\n        return fmt.Sprintf(\"asteria.%s.log\", time.Now().Format(\"20060102\"))\n    })\n    log.Writer(fw)\n\n\n#### Syslog\n\n    // Set the default module log output\n    log.Writer(writer.NewSyslogWriter(\"\", \"\", syslog.LOG_DEBUG | syslog.LOG_SYSLOG, \"asteria\"))\n    // can also be like this\n    log.Default().Writer(writer.NewSyslogWriter(\"\", \"\", syslog.LOG_DEBUG | syslog.LOG_SYSLOG, \"asteria\"))\n    // Set the log format of the specified module\n    log.Module(\"asteria\").Writer(writer.NewSyslogWriter(\"\", \"\", syslog.LOG_DEBUG | syslog.LOG_SYSLOG, \"asteria\"))\n\n#### Stream\n\n    // Set the default module log output\n    log.Writer(writer.NewStreamWriter(os.Stdout))\n    // can alse be like this\n    log.Default().Writer(writer.NewStreamWriter(os.Stdout))\n    // Set the log format of the specified module\n    log.Module(\"asteria\").Writer(writer.NewStreamWriter(os.Stdout))\n\n#### Stack\n\nIf you want to write the log to multiple different outputs, you can use `StackWriter`\n\n    m1 := writer.NewStdoutWriter()\n    m2 := writer.NewSyslogWriter(\"\", \"\", syslog.DEBUG, \"asteria\")\n    m3 := writer.NewDefaultFileWriter(\"/var/log/asteria.log\")\n\n    stack := writer.NewStackWriter()\n    // only write debug log to m1\n    stack.PushWithLevels(m1, level.Debug)\n    // only write error and emergency log to m2\n    stack.PushWithLevels(m2, level.Error, level.Emergency)\n    // all logs will write to m3\n    stack.PushWithLevels(m3)\n    \n    log.Writer(stack)\n    // Or\n    log.Default().Writer(stack)\n    // Or\n    log.Module(\"asteria\").Writer(stack)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmylxsw%2Fasteria","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmylxsw%2Fasteria","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmylxsw%2Fasteria/lists"}