From 9671b138db24a11b0be7244a22b3b0741c70d505 Mon Sep 17 00:00:00 2001 From: Krzysztof Reczek Date: Thu, 23 Sep 2021 17:20:49 +0200 Subject: [PATCH] Add view rendering logs (#52) --- README.md | 2 +- cmd/example-yaml/example.sh | 3 +++ cmd/example-yaml/scraper.yaml | 1 - cmd/example/example.sh | 3 +++ cmd/example/main.go | 1 - pkg/scraper/config.go | 3 --- pkg/scraper/log.go | 2 +- pkg/scraper/yaml.go | 4 +--- pkg/view/log.go | 23 +++++++++++++++++++++++ pkg/view/render.go | 6 ++++++ pkg/yaml/yaml.go | 1 - pkg/yaml/yaml_test.go | 2 -- 12 files changed, 38 insertions(+), 13 deletions(-) create mode 100644 pkg/view/log.go diff --git a/README.md b/README.md index 237f146..3ab443a 100644 --- a/README.md +++ b/README.md @@ -190,7 +190,7 @@ err = v.RenderStructureTo(structure, outFile) ``` ## Debug mode -In order to see detailed scraping logs, set `logDebug` flag in the scraper configuration or set `LOG_LEVEL` env variable with `debug` of `DEBUG`. +In order to see detailed scraping or view rendering logs, set `LOG_LEVEL` env variable with `debug` of `DEBUG`. ## Examples You may find a couple of examples implemented in the `cmd` directory. In order to run any of those examples, please run the shell script attached. diff --git a/cmd/example-yaml/example.sh b/cmd/example-yaml/example.sh index 2000b45..0ff0bfb 100644 --- a/cmd/example-yaml/example.sh +++ b/cmd/example-yaml/example.sh @@ -2,6 +2,9 @@ set -e +# Uncomment the line below to see detailed execution logs +#export LOG_LEVEL=debug + rm -rf .out && mkdir .out go run main.go diff --git a/cmd/example-yaml/scraper.yaml b/cmd/example-yaml/scraper.yaml index 0dbaf5d..7ae2dd7 100644 --- a/cmd/example-yaml/scraper.yaml +++ b/cmd/example-yaml/scraper.yaml @@ -4,7 +4,6 @@ configuration: - "net/http" - "go.uber.org/zap" - "google.golang.org/grpc" - log_debug: true rules: - name_regexp: ".*Handler.*" diff --git a/cmd/example/example.sh b/cmd/example/example.sh index 2000b45..0ff0bfb 100644 --- a/cmd/example/example.sh +++ b/cmd/example/example.sh @@ -2,6 +2,9 @@ set -e +# Uncomment the line below to see detailed execution logs +#export LOG_LEVEL=debug + rm -rf .out && mkdir .out go run main.go diff --git a/cmd/example/main.go b/cmd/example/main.go index 41c1ccf..dfa06bc 100644 --- a/cmd/example/main.go +++ b/cmd/example/main.go @@ -48,7 +48,6 @@ func buildScraper() scraper.Scraper { "go.uber.org/zap", "google.golang.org/grpc", ) - c.LogDebug = true s := scraper.NewScraper(c) diff --git a/pkg/scraper/config.go b/pkg/scraper/config.go index 3a14147..bb14d38 100644 --- a/pkg/scraper/config.go +++ b/pkg/scraper/config.go @@ -7,11 +7,8 @@ package scraper // package prefixes is omitted and its internal structure is not scraped. // When no package prefix is provided, the scraper will stop // scraping given structure on a root level. -// -// LogDebug flag makes the scraper print details execution logs to the console. type Configuration struct { Packages []string - LogDebug bool } // NewConfiguration instantiates Configuration with a set of package diff --git a/pkg/scraper/log.go b/pkg/scraper/log.go index 1fa8866..60f8340 100644 --- a/pkg/scraper/log.go +++ b/pkg/scraper/log.go @@ -9,7 +9,7 @@ import ( func (s *scraper) isDebugMode() bool { level := os.Getenv("LOG_LEVEL") - return level == "DEBUG" || level == "debug" || s.config.LogDebug + return level == "DEBUG" || level == "debug" } func (s *scraper) debug(v reflect.Value, format string, a ...interface{}) { diff --git a/pkg/scraper/yaml.go b/pkg/scraper/yaml.go index 4fd3963..bee55d3 100644 --- a/pkg/scraper/yaml.go +++ b/pkg/scraper/yaml.go @@ -9,9 +9,7 @@ import ( ) func toScraperConfig(c yaml.Config) Configuration { - config := NewConfiguration(c.Configuration.Packages...) - config.LogDebug = c.Configuration.LogDebug - return config + return NewConfiguration(c.Configuration.Packages...) } func toScraperRules(c yaml.Config) ([]Rule, error) { diff --git a/pkg/view/log.go b/pkg/view/log.go new file mode 100644 index 0000000..e3bd794 --- /dev/null +++ b/pkg/view/log.go @@ -0,0 +1,23 @@ +package view + +import ( + "fmt" + "log" + "os" + + "github.com/krzysztofreczek/go-structurizr/pkg/model" +) + +func (v view) isDebugMode() bool { + level := os.Getenv("LOG_LEVEL") + return level == "DEBUG" || level == "debug" +} + +func (v view) debug(c model.Component, format string, a ...interface{}) { + if !v.isDebugMode() { + return + } + + m := fmt.Sprintf(format, a...) + log.Printf("[%s][id: %s] %s\n", c.Name, c.ID, m) +} diff --git a/pkg/view/render.go b/pkg/view/render.go index e6aa197..b747d4c 100644 --- a/pkg/view/render.go +++ b/pkg/view/render.go @@ -79,6 +79,7 @@ func (v view) resolveExcludedComponentIDs(s model.Structure) map[string]struct{} ids := map[string]struct{}{} for _, c := range s.Components { if !v.hasComponentTag(c.Tags...) { + v.debug(c, "component will be excluded from the view") ids[c.ID] = struct{}{} } } @@ -90,6 +91,7 @@ func (v view) renderRootComponents(ctx *context) { if !v.isRoot(c.Tags...) { continue } + v.debug(c, "component has been recognised as root element") v.renderComponent(ctx, c, "") } } @@ -141,6 +143,8 @@ func (v view) renderComponent(ctx *context, c model.Component, parentID string) group := groupID(parentID, shapeStyle, ctx.level) + v.debug(c, "rendering component with shape '%s', shape style '%s', and group '%s'", shape, shapeStyle, group) + ctx.sb.WriteString(buildComponent(c, shape, shapeStyle, group)) ctx.renderedIDs[c.ID] = struct{}{} } @@ -156,6 +160,8 @@ func (v view) renderRelation(ctx *context, srcID string, trgID string) { return } + v.debug(ctx.s.Components[srcID], "rendering relation to component of id '%s'", trgID) + ctx.sb.WriteString(buildComponentConnection(srcID, trgID, v.lineColor)) ctx.renderedRelations[relationID] = struct{}{} } diff --git a/pkg/yaml/yaml.go b/pkg/yaml/yaml.go index ea74f6e..0f3e3e7 100644 --- a/pkg/yaml/yaml.go +++ b/pkg/yaml/yaml.go @@ -17,7 +17,6 @@ type Config struct { // ConfigConfiguration is an open YAML configuration structure. type ConfigConfiguration struct { Packages []string `yaml:"pkgs"` - LogDebug bool `yaml:"log_debug"` } // ConfigRule is an open YAML configuration structure. diff --git a/pkg/yaml/yaml_test.go b/pkg/yaml/yaml_test.go index 15b633a..ec59201 100644 --- a/pkg/yaml/yaml_test.go +++ b/pkg/yaml/yaml_test.go @@ -13,7 +13,6 @@ const ( testYAMLConfiguration = ` configuration: pkgs: [PKG_1, PKG_2] - log_debug: true ` testYAMLRules = ` @@ -65,7 +64,6 @@ func TestLoadFrom(t *testing.T) { expected: yaml.Config{ Configuration: yaml.ConfigConfiguration{ Packages: []string{"PKG_1", "PKG_2"}, - LogDebug: true, }, }, },