Skip to content

Commit

Permalink
Add view rendering logs (#52)
Browse files Browse the repository at this point in the history
  • Loading branch information
krzysztofreczek authored Sep 23, 2021
1 parent d39e915 commit 9671b13
Show file tree
Hide file tree
Showing 12 changed files with 38 additions and 13 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
3 changes: 3 additions & 0 deletions cmd/example-yaml/example.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion cmd/example-yaml/scraper.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ configuration:
- "net/http"
- "go.uber.org/zap"
- "google.golang.org/grpc"
log_debug: true

rules:
- name_regexp: ".*Handler.*"
Expand Down
3 changes: 3 additions & 0 deletions cmd/example/example.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion cmd/example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ func buildScraper() scraper.Scraper {
"go.uber.org/zap",
"google.golang.org/grpc",
)
c.LogDebug = true

s := scraper.NewScraper(c)

Expand Down
3 changes: 0 additions & 3 deletions pkg/scraper/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion pkg/scraper/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}) {
Expand Down
4 changes: 1 addition & 3 deletions pkg/scraper/yaml.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
23 changes: 23 additions & 0 deletions pkg/view/log.go
Original file line number Diff line number Diff line change
@@ -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)
}
6 changes: 6 additions & 0 deletions pkg/view/render.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}{}
}
}
Expand All @@ -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, "")
}
}
Expand Down Expand Up @@ -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{}{}
}
Expand All @@ -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{}{}
}
Expand Down
1 change: 0 additions & 1 deletion pkg/yaml/yaml.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 0 additions & 2 deletions pkg/yaml/yaml_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ const (
testYAMLConfiguration = `
configuration:
pkgs: [PKG_1, PKG_2]
log_debug: true
`

testYAMLRules = `
Expand Down Expand Up @@ -65,7 +64,6 @@ func TestLoadFrom(t *testing.T) {
expected: yaml.Config{
Configuration: yaml.ConfigConfiguration{
Packages: []string{"PKG_1", "PKG_2"},
LogDebug: true,
},
},
},
Expand Down

0 comments on commit 9671b13

Please sign in to comment.