Skip to content

Function log()

📝 Description

The log function returns the logarithm of a given number with a specified base. It answers the question: "To what power must the base be raised to produce this number?"

Note: Both arguments must be positive numbers. The base must not be 1.


💻 Syntax

log(number, base)

💡 Examples

1. Base 2 (Binary Logarithm):

# Result: 3 (because 2^3 = 8)
output "binary_log" {
  value = log(8, 2)
}

2. Base 10 (Common Logarithm):

# Result: 2 (because 10^2 = 100)
output "common_log" {
  value = log(100, 10)
}

3. Scaling Calculation (Real-world scenario):

Imagine you want to calculate how many "layers" of infrastructure you need based on the number of nodes, where each layer can handle 4 times more nodes than the previous one:

locals {
  total_nodes = 64
  infra_layers = log(local.total_nodes, 4) # 4^3 = 64 -> Result: 3
}

⚠️ Common Errors

Input Result Why?
log(-10, 2) Error The number must be positive.
log(10, 1) Error The base cannot be 1.
log(10, -2) Error The base must be positive.

  • [[pow]] — The inverse of log: raises a number to a power.

  • [[ceil]] — Use this with log to get a whole number of "layers" (e.g., ceil(log(50, 4))).

  • Official HashiCorp Docs