https://github.com/shufo/activity_log
A logger with Activitiy Streams like format for Elixir
https://github.com/shufo/activity_log
elixir
Last synced: over 1 year ago
JSON representation
A logger with Activitiy Streams like format for Elixir
- Host: GitHub
- URL: https://github.com/shufo/activity_log
- Owner: shufo
- Created: 2017-10-16T05:22:01.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2023-12-15T11:56:12.000Z (over 2 years ago)
- Last Synced: 2024-05-01T21:22:11.127Z (about 2 years ago)
- Topics: elixir
- Language: Elixir
- Homepage: https://hex.pm/packages/activity_log
- Size: 11.7 KB
- Stars: 2
- Watchers: 3
- Forks: 0
- Open Issues: 8
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# ActivityLog
Logging activities with [Activitiy Streams](https://www.w3.org/TR/activitystreams-core/) like format (not the same, but inspired).
## Installation
```elixir
def deps do
[
{:activity_log, "~> 0.1.0"}
]
end
```
## Usage
Define your activities.
```elixir
defmodule MyApp.Activity.Article do
use ActivityLog
activity "create" do
actor :user
object :article
end
def name(actor, object), do: "#{actor.name} created #{object.name}"
end
```
Create activity.
```elixir
iex> alias MyApp.Activity.Article
iex> activity = %Article{actor: %Article.Actor{id: 1, name: "foo"}, object: %Article.Object{id: 1, name: "My article"}}
iex> ActivityLog.add(activity)
# outputs
05:29:32.128 [info] {"type":"create","target":null,"object":{"type":"article","name":"My article","id":1},"name":"foo createed My article","actor":{"type":"user","name":"foo","id":1},"@timestamp":"2017-10-15T20:29:32.128192Z","@context":"https://github.com/shufo/activity_log"}
:ok
```
## How to
### Define target
If you want to add the `target` property, define target in your activity module.
```elixir
defmodule MyApp.Activity.Comment do
use ActivityLog
activity "add" do
actor :user
object :comment
target :article
end
def name(actor, object, target), do: "#{actor.name} commented #{object.name} to #{target.name}"
end
iex> alias MyApp.Activity.Comment
# create activity
iex> activity = %Comment{actor: %Comment.Actor{id: 1, name: "foo"}, object: %Comment.Object{id: 2, name: "Nice article!"}, target: %Comment.Target{id: 3, name: "My article"}}
iex> ActivityLog.add(activity)
# => 05:27:34.877 [info] {"type":"add","target":{"type":"article","name":"My article","id":3},"object":{"type":"comment","name":"Nice article!","id":2},"name":"foo commented Nice article! to My article","actor":{"type":"user","name":"foo","id":1},"@timestamp":"2017-10-15T20:27:34.877639Z","@context":"https://github.com/shufo/activity_log"}
:ok
```
### Change Logger
Default logger is Elixir's built-in [Logger](https://hexdocs.pm/logger/Logger.html).
If you want to change the logging backend, define your logger and configuration.
```elixir
config :activity_log, backends: [YourApp.Logger]
```
You need to implement the `Activity.Logger` behaviour. (`add/2`)
```elixir
defmodule YourApp.Logger do
def add(activity, opts \\ []) do
# do something
end
end
```